String Functions in C with Examples

String Functions in C with Examples

C string functions; Through this tutorial, you will learn everything about string functions in c programming language with the help of examples.

String Functions in C

  • What are string functions in C
  • List of String Functions in C
  • Examples of String Functions in C
    • C program to copy one string to another
    • C program for concatenate two strings
    • C program to find the length of String using strlen()
    • C program to reverse a string using strrev
    • C program to compare two strings using strcmp
    • C program to convert string in lower case

What are string functions in C

String functions are used to manipulate a string in the C programming language.

For example; If you want to get the length of the string in the C program. For this, you have to use strlen() string function.

List of String Functions in C

There are list of string functions in c; as follows:

FunctionSyntax (or) ExampleDescription
strcpy()strcpy(string1, string2)Copies string2 value into string1
strncpy()strncpy(string1, string2, 5)Copies first 5 characters string2 into string1
strlen()strlen(string1)returns total number of characters in string1
strcat()strcat(string1,string2)Appends string2 to string1
strncat()strncpy(string1, string2, 4)Appends first 4 characters of string2 to string1
strcmp()strcmp(string1, string2)Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if string1>string2
strncmp()strncmp(string1, string2, 4)Compares first 4 characters of both string1 and string2
strcmpi()strcmpi(string1,string2)Compares two strings, string1 and string2 by ignoring case (upper or lower)
stricmp()stricmp(string1, string2)Compares two strings, string1 and string2 by ignoring case (similar to strcmpi())
strlwr()strlwr(string1)Converts all the characters of string1 to lower case.
strupr()strupr(string1)Converts all the characters of string1 to upper case.
strdup()string1 = strdup(string2)Duplicated value of string2 is assigned to string1
strchr()strchr(string1, ‘b’)Returns a pointer to the first occurrence of character ‘b’ in string1
strrchr()‘strrchr(string1, ‘b’)Returns a pointer to the last occurrence of character ‘b’ in string1
strstr()strstr(string1, string2)Returns a pointer to the first occurrence of string2 in string1
strset()strset(string1, ‘B’)Sets all the characters of string1 to given character ‘B’.
strnset()strnset(string1, ‘B’, 5)Sets the first 5 characters of string1 to the given character ‘B’.
strrev()strrev(string1)It reverses the value of string1

Examples of String Functions in C

  • C program to copy one string to another
  • C program for concatenate two strings
  • C program to find the length of String using strlen()
  • C program to reverse a string using strrev
  • C program to compare two strings using strcmp
  • C program to convert string in lower case

C program to copy one string to another

Using the string strcpy() function; you can copy the contents of the string one into another string in c. See the following example:

#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int  len ;
/* copy str1 into str3 */
strcpy(str3, str1);
}

C program for concatenate two strings using strcat

Using the string strcat() function; you can concatenate or add two strings in c. See the following example:

#include <stdio.h>
#include <string.h>
int main () {
 char str1[12] = "Hello";
 char str2[12] = "World";
 strcat( str1, str2);
 printf("The concatenated string is : %s",str1);
}

C program to find the length of String using strlen()

Using the string strlen() function; you can find the length of given strings in c program. See the following example:

#include <stdio.h>
#include <string.h>
int main () {
    char str1[12] = "Hello";
    int  len ;
    len = strlen(str1);
    printf("strlen(str1) :  %d\n", len );
}

C program to reverse a string using strrev

Using the string strrev() function; you can reverse a given string in the c program. See the following example:

1#include <stdio.h>  
#include <string.h>  
int main()  
{  
    char str[40]; // declare the size of character string  
    printf (" \n Enter a string to be reversed: ");  
    scanf ("%s", str);  
      
    // use strrev() function to reverse a string  
   printf (" \n After the reverse of a string: %s ", strrev(str));  
    return 0;  
}

C program to compare two strings using strcmp

Using the string strcmp() function; you can compare two strings in the c program. See the following example:

#include<stdio.h>
#include <string.h>
    int main(){
    char str1[10],str2[10];
    gets(str1);
    gets(str2);
    if(strcmp(str1,str2)==0)
    printf("Strings :equal");
    else
    printf("Strings: not equal");
}

C program to convert string in upper case

Using the string strupr() function; you can convert the string into upper case in the c program. See the following example:

#include<stdio.h>
#include <string.h>
int main(){
    char str[20];
    gets(str);
    printf("String is: %s",str);
    printf("\nUpper String is: %s",strupr(str));
}

C program to convert string in lower case

Using the string strlwr() function; you can convert the string into lower case in the c program. See the following example:

#include<stdio.h>
#include <string.h>
int main(){
    char str[20];
    gets(str);
    printf("String is: %s",str);
    printf("\nUpper String is: %s",strlwr(str));
}

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *