String handling Functions

  • We will see some standard library functions, which works on strings.
  • This Functions operates on strings.
  • They are used to handle the strings.
  • For using this functions ,We have to specify the header file #include<string.h>
Functions and their description.
#include<string.h>
Function Description
strlen(s) Gives the lenght of the string(number of character in string).
strcpy(s1,s2) Copy s2 string to string s1.
strcmp(s1,s2) Compare both string and return 0 if both the string are equal
return positive value if s1 is greater than s2 and return negative value if s1 is less than s2
strrev(s) returns the string in Reverse order.
strcat(s1,s2) Concate the string s2 to s1
strncmp(s1,s2,n) Compare n numbers of character from both the string and return 0 if both the string are equal
return positive value if s1 is greater than s2 and return negative value if s1 is less than s2
strcmpi(s1,s2) Compare both string but ignore the cases and return 0 if both the string are equal
return positive value if s1 is greater than s2 and return negative value if s1 is less than s2
strchr(s1,c) Returns a pointer to the first occurence of the character c in string s1.
strrchr(s1,c) Returns a pointer to the Last occurence of the character c in string s1.
strstr(s1,s2) Returns a pointer to the first occurence of the string s2 in string s1.
strupr(s1) Converts all the characters of string s1 with uppercase characters.
strlwr(s1) Converts all the characters of string s1 with lowercase characters.

Let's understand above functions with example.

Strlen(s1) Function:
  • This function gives the length of the string.
  • Counts the number of character in string

Example:

#include<stdio.h>

#include<string.h>

void main()
{
   char s1[10] = "Programmer";
   int lenght;
   
//using strlen() function with passing s1 string to it
//and assigning  it to a variable length. 
   lenght = strlen(s1);
   
   printf("length of string s1 is %d",lenght);
   
}

Output:
length of string s1 is 10.

Strcpy(s1,s2) Function:
  • This function Copy second string to the first string
  • This function is used to assign any string to an another string.

Example:

#include<stdio.h>

#include<string.h>

void main()
{
   char s1[15] = "Programmer";
   char s2[15];

   //Copying the s1 string to s2.
   //string s1 will remain same.
   strcpy(s2,s1);
    //printing string s2
   printf("string s2 is %s.",s2);
   
}

Output:
string s2 is Programmer.

Strrev(s1) Function:
  • This function Reverse the string.
  • It helps to print the string in reverse order.

Example:

#include<stdio.h>

#include<string.h>

void main()
{
   char s1[15] = "Programmer";
   char s2[15];

   //copying the returned value of strrev() funtion to s2
   //string, by using strcpy() function.
   strcpy(s2,strrev(s1));

    //printing reverse string
   printf("s1 string in Reverse order is  %s.",s2);

}

Output:
s1 string in Reverse order is remmargorP.

Strcat(s1,s2) Function:
  • This function Concat one string to another string.
  • This function concat second string(s2) to first string(s1).

Example:

#include<stdio.h>

#include<string.h>

void main()
{
   char s1[15] = "Programmers";
   char s2[15] = " are great";

   //concatinating string s2 to s1 string
   //using strcat() function . 
   strcat(s1,s2);
    //printing concated string. 
   printf("Concated  string is  %s.",s1);

}

Output:
Concated string is Programmers are great.

Strlwr(s1) & strupr(s1) Functions:
  • Function strlwr() converts the strings character in lowercase characters.
  • Function strupr() converts the strings character in uppercase characters.

Example:

#include<stdio.h>

#include<string.h>

void main()
{
   char s1[15] = "Programmers";
   char lower[15],upper[15];

   //copying the returned value of strlwr() funtion to lower
   //string, by using strcpy() function.
   strcpy(lower,strlwr(s1));


    //copying the returned value of strupr() funtion to upper
   //string, by using strcpy() function.
    strcpy(upper,strupr(s1));
    //printing lowercase string.
    printf("\nString s1 in lowercase characters is %s",lower);


    //printing uppercase string.
    printf("\nString s1 in uppercase characters is %s",upper);
}

Output:
String s1 in lowercase characters programmers
String s1 in uppercase characters PROGRAMMERS.

Strcmp(s1,s2) & strcmpi(s1,s2) & strncmp(s1,s2) Functions:
  • Function strcmp(s1,s2)compares two string and return positive value if s1 is greater than s2,reutrn 0 if both are equal,return negative value if s1 less than s2
  • Function strncmp()Compares n numbers of character from both stringsand returns same value like strcmp()
  • Function strcmpi()Compares the both strings but ignores case and returns same value like strcmp()

Example:

#include<stdio.h>

#include<string.h>

void main()
{
   char s1[15] = "Hello World";
   char s2[15] = "Hello world";

   int cmp,cmpi,cmpn;
  //
  cmp = strcmp(s1,s2);

  //this function will compare 5 character
  //and return the result.
  cmpn = strncmp(s1,s2,5);

  //ignores the case and return result
  cmpi = strcmpi(s1,s2);


  printf("\nUsing strcmp() result is %d",cmp);
   printf("\nUsing strncmp() result is %d",cmpn);
    printf("\nUsing strcmpi() result is %d",cmpi);

}

Output:
Using strcmp() result is -1
Using strncmp() result is 0.
Using strcmpi() result is 0

  • This is how string handling functions works.
  • Play around the string handling functions to understand the concept more betterly.





Further Concepts: