string functions in c

string functions are the functions that satisfy the logic needed to perform some operations.
string functions are defined using  #include<string.h>.

the following table shows eighteen string functions in c and, these eighteen string functions in C are explained in detail further.



1.  strlen

 2. strcpy

3. strcat

 4. strncat

5.  strcmp

6.  strncmp

7.  strcmpi/stricmp

8.  strnicmp

 9. strupr

10.  strlwr

11.  strrev

12. strdup

13.  stndup

14.  strset

15. strnset

16. strchr

 17. strrchr

18.  strncpy




Let us see every function one by one

note: every program which consists of a string function in it should  define #include<stdio.h> header file at the top of the program. 

1.  strlen  

 strlen is used to find the length of the string.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  char len[]=banana;
  int length1,length2;
  length1=strlen(len);
  length2=strlen(god is great);
  printf(length of banana is %d\n,  length1);
  printf(length of god is great is %d,length2);
getch();
}

Output::  length of banana is 6 

               length of god is great is 12

2.  strcpy 

    strcpy is used to copy the one string into another string 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char string1[]=banana;
 char string2[10];
 strcpy(string2,string1);
 printf( string1= %s\n, string1);
 printf(string2=%s, string2);
 getch();
}
 

Output:: string1=banana

                String2=banana

3.  strcat 

  strcat is used to append one string at the end of another string 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 #include<stdio.h>
 #include<conio.h>
 #include<string.h>
 void main()
  {
   char string1[20]=banana;
   char string2[]=apple;
   strcat(string1,string2);
   printf(string1=%s\n, string1);
   printf(string2=%s, string2);
 getch();
}

Output:: string1=bananaapple

                String2=apple

4.  Strncat

It is used to append the first n characters of the string at the end of another string.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 #include<stdio.h>
 #include<conio.h>      
  #include<string.h>
 void main()
 {
  char st1[15]="mango ";
  char st2[]="grapes are sweet";
  strncat(st1,st2,6); //this is the   syntax for appending
   printf("%s ",st1);
 getch();
}
Output:: mango grapes
  •  In the above program, we have used two strings, st1 and st2.
  •  We have appended the first 6 characters of st2 at the end of st1

5.   strcmp

strcmp is used to compare two strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 #include<stdio.h>
 #include<conio.h> 

#include<string.h>
 void main()
{
 char string1=banana;
 char string2=sunday;
 int l,m,n;
 l= strcmp(string1,banana);
 m=strcmp(string2,funday);
 n=strcmp(string2,Sunday holiday);
 getch();
}
 Output: 0  13  -32 
  • After comparing the two strings if the output is zero then two strings are equal, the value stored in the ‘l’ variable is the output of the comparison of two strings which are the same.
  •  The value store in the variable ‘m’ is 13 which is the difference between the  ASCII value of the alphabet ‘s’ and ‘f’. this alphabet is the first alphabet of the two strings compared.
  • The value stored in the variable ‘n’ is -32  which is the difference between the ASCII value of the null character’\0’ of string2 and the space in the string compared “Sunday holiday”. The ASCII value of space is 32 and the ASCII value of ‘\0’ is 0 hence, 0-32=32.

6.  strncmp

strncmp is used to compare the first n characters of the two strings 

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  char string1=god ;
  char string2=god is great;
  int l;
  l= strncmp(string1,string2,4);
  printf(%d ,l);
  getch();
 }

Output:: 0

  • In this program, the output is zero.
  • Here we have compared the first four characters of string1 and string2.
  • The first four characters of string1 and string2 are equal that’s why the output is zero.
  • The output zero came due to the calculation that, the ASCII value of the character string1 minus string2 is zero. 
  •  Now if the ASCII value of string1 – string2 was greater than zero then, both the strings compared were unequal and output was greater than zero.
  • if the ASCII value of string1 – string2 was smaller than zero then, both the strings compared were unequal and output was less than zero.

7.  strcmpi/stricmp

strcmpi is used to compare the two strings by ignoring the case i.e lowercase or upper case

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  char string1[]="mango is sweet";
  char string2[]="MANGO is SWEET";
  int i,j,k;
  clrscr();
  i=strcmpi(string1,string2);
  j=strcmpi(string1,"mango IS best");
  k=strcmpi(string1,"MANGO is wanted");
  printf("%d %d %d", i,j,k);
  getch();
  } 

Output::0 17 -4

  • In this program, we have compared two strings without considering the cases of the strings.
  • Variables i,j,k are used to store the output of the comparison between two strings.
  • The output of i is 0 because both the string are equal/same, the comparison is done by checking the difference between the ASCII values of two strings and if the difference is 0 then both the string are the same. Here string1 and string2 are the same.
  • The output of j is 17 because the difference between the ASCII value of the 10th character of string1 and another string compared is 17.
  • The output of k is -4 because the difference between 10th the ASCII value of the 10th character of string1 and another string compared is -4.

8.  strnicmp

strnicmp is used to compare first n chaeracters of two strings without considering the  case

#include<stdio.h>
#include<conio.h>
#include<string.h>
 void main()
 {
  char string1[]="mango is sweet";
  char string2[]="MANGO is SWEET";
  int i,j,k;
  clrscr();
  i=strnicmp(string1,string2,6);
  j=strnicmp(string1,"apple IS best",6);
  k=strnicmp(string1,"Orange is sweet",6);
  printf("%d %d %d", i,j,k);
  getch();
 } 

  Output:: 0 12 -2

  • In this program, we have compared the first n two strings without considering the cases of the strings.
  • Here that nth character is 6. 
  •  Variables i,j,k are used to store the output of the comparison between two strings.
  • The output of i is 0 because, both the string till the 6th character is equal/same, the comparison is done by checking the difference between the ASCII values of two strings and if the difference is 0 then both the string are the same. Here string1 and string2 till nth character are the same.
  • The output of j is 12 because the difference between the ASCII value of the 1st character of string1 and another string compared is 12. 
  • The output of k is -2 because the difference between the ASCII value of the 1st character of string1 and another string compared is -2.

9.  strupr

strupr is used to convert the string into upper case

#include<stdio.h>
#include<conio.h>
#include<string.h>
 void main()
  {
   char string[]="mango";
   clrscr();
   strupr(string);
   printf(" converted into upper case: %s ",string);
   getch();
  } 

Output:: converted into upper case: MANGO

10.  Strlwr

Strlwr is used to convert the string into lowercase

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void main()
  {
   char string[]="GOD IS GREAT";
   clrscr();
   strlwr(string);
   printf(" converted into lower case: %s ",string);
   getch();
  } 

Output:: converted into upper case: MANGO

11.  strrev 

strrev is used to reverse the string 

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void main()
  {
   char string[]="best of luck";
   clrscr();
   strrev(string);
   printf(" reverse string: %s ",string);
   getch();
  } 

Output:: reverse string:: kcul fo tseb

12.   strdup

strdup is used to provide a duplicate string of a given string

#include<stdio.h>
#include<conio.h>
#include<string.h>
 void main()
   {
    char string1[]="god is great";
    char*string2=strdup(string1);
    clrscr();
    printf("this is the duplicate  string:: %s",string2);
    getch();
   }

Output:: this is the duplicate string:: god is great

  • A duplicate copy of string1 is created dynamically and a pointer string2 to copy is returned.

13.   strndup

strndup provides a duplicate string of a given string till nth character only.

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void main()
{
char string1[]="god is great";
char* string2=strndup(string1, 4); //this is syntax
clrscr();
printf("this is duplicate string till 4th character:: %s",string2);
getch();
}

Output:: this is duplicate string till 4th character:: god

14.  strset 

strset is used to set all the characters of string to given character

#include<stdio.h>
#include<conio.h>
#include<string.h>
    void main()
    {
     char string[]="god is great";
     clrscr();
     printf("string before setting characters: %s\n",string);
     strset(string,'*'); // this is syntax for strset
     printf("set characters: %s",string);
     getch(); 
    }

Output:: set characters: ************

  In this program, the characters of the string are replaced by ‘*’ character.

15.  strnset 

strnset sets first n characters of string to given character.

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void main()
    {
     char string[]="god is great";
     clrscr();
     printf("string before setting characters: %s\n",string);
     strnset(string,'#',4);
     printf("set characters: %s",string);
     getch();
  }

Output:: set characters:: ####is great

In this program, the first 4 characters of the string are replaced by the # character.

16.  strchr

strchr finds first occurrence of given character in string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
   void main()
   {
    char string1[] ="god is great";
    char* string2;
    clrscr();
    string2=strchr(string1,'d');
    printf("dynamic string2 is::  %s\n", string2);
    printf("position of first'd' is: %d\n",string1-string2+1);
    printf("The first occurence of the character 'd' of string '%s' is %s\n",string1,string2);
    getch();
   }   

Output:: dynamic string2 is: d is great

                 Position of first ‘d’ is: 3

                  The first occurrence of the character ‘d’ of string1 ‘god is great’ is g is great

17.  strrchr 

   strrchr finds last occurrence of given character in the string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
   void main()
   {
    char string1[] ="god is great";
    char* string2;
    clrscr();
    string2=strrchr(string1,'g'); //syntax
    printf("dyanamic string2 is %s\n", string2);
    printf("position of last 'g' is: %d\n",string2-string1+1);
    printf("The last occurance of the character 'g' of string1 ‘%s' is %s\n",string1,string2);
    getch();
  }

Output:: dynamic string2 is great

                 Position of last ‘g’ is: 8

                 The last occurrence of the character ‘g’ of string1 ‘god is great’ is great

18.  strncpy

strncpy copies the first n characters of one string into another

#include<stdio.h>
#include<conio.h>
#include<string.h>
   void main()
{
 char string1[]=banana;
 char string2[10];
 strncpy(string2,string1,3);
 printf( string1= %s\n, string1);
 printf(string2=%s, string2);
 getch();
}

Output:: string1=banana

                 String2=ban


Refer also::