• C programming language provides a rich set of string handling functions to perform different task on string easily. They are available inside string.h header file so we need to include string.h into our program to use them. In this article, we will look into some of the most common string functions in detail.

strlen()

Use: It is used to find out length of a given string that means it counts total number of characters of a given string. It counts total characters including space.

Syntax:

      int strlen(string)

Example:

  int n = strlen("Hexainclude");
  printf("\n Length of a string is : %d" , n ); 

Output:

  Length of a string is : 11

strcmp()

Use: It is used to compare two strings. It compares one by one character from both strings until it found non matching character. If both strings are same, it returns 0 else its ascii difference is return. If first string is greater than second then ascii difference is in positive and negative if it is smaller than second string.

Syntax:

 int strcmp(string1, string2)

Example:

 char str1[] = “hello” ;
 char str2[] = “hello” ;
 
 int n = strcmp(str1 , str2) ;
 
 if (n == 0)
    printf(“\n Both strings are same!”);
 else
    printf(“\n Both strings are different!”);

Output :

 Both strings are same!

strcmpi()

Use: Similar to strcmp() it is used to compare two strings without case sensitivity. It compares one by one character from both strings until it found non matching character. If both strings are same, it returns 0 else its ascii difference is return. If first string is greater than second then ascii difference is in positive and negative if it is smaller than second string.

Syntax:

 int strcmpi(string1, string2)

Example:

 char str1[] = “Hexainclude” ;
 char str2[] = “HEXAINCLUDE” ;
 
 int n = strcmpi(str1 , str2) ;
 
 if (n == 0)
    printf(“\n Both strings are same!”);
 else
    printf(“\n Both strings are different!”);

Output :

 Both strings are same!

strcpy()

Use: It is used to copy one string to another.

Syntax:

 strcpy ( destination_string , source_string )

Example:

 char str1[] = “hello” ;
 char str2[20] ;

 strcpy(str2 , str1) ;     // copy str1 to str2
    printf(“\n String 2 is %s”,  str2);

Output:

 String 2 is hello

strcat()

Use: It is used to join or concatenate one string to another. It appends second string at the end of the first string. By default it does not add space so we explicitly need to add space at the time of concatenate.

Syntax:

 strcat (destination_string , source_string)

Example:

 char str1[] = “Welcome to  ” ;
 char str2[] = “Hexainclude!” ;
 
 strcat (str1 , str2) ;

 printf(“\n String 1 after concatenate is %s”,  str1);

Output:

 String 1 after concatenate is Welcome to Hexainclude!

strrev()

Use: It is used to reverse all characters of a given string except the terminating null character. It returns pointer to reverse string.

Syntax:

char * strrev(string)

Example:

char str[] = "Hexainclude" ;

printf("\n String before reverse is %s " , str );
strrev(str) ;
printf("\n String after calling strrev() is %s " , str ) ;

Output:

String before reverse is Hexainclude
String after calling strrev() is edulcniaxeH

gets()

Use: It is used to read string from the terminal until newline character is found.

Syntax:

gets (string)

Example:

char str[50] ;

printf(“\n Enter String :") ;
gets(str);
printf(“\n String is %s “, str);

Output:

Enter String : Welcome to Hexainclude!
String is Welcome to Hexainclude!

puts()

Use: It is used to write (print) string on the terminal until newline character is found.

Syntax:

puts (string)

Example:

char str[50] ;

printf(“\n Enter String :) ;
gets(str) ;
puts(str) ;

Output:

Enter String : Welcome! to Hexainclude!
Welcome to Hexainclude!

Leave a Reply

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