Explain string library functions with suitable examples in C

CServer Side ProgrammingProgramming

String Library functions

The predefined functions which are designed to handle strings are available in the library “string.h”. They are −

  • strlen ()
  • strcmp ()
  • strcpy ()
  • strncmp ()
  • strncpy ()
  • strrev ()
  • strcat ()
  • strstr ()
  • strncat ()

The strlen () function

It returns the number of characters in a string.

Syntax

int strlen (string name)

Example

#include <string.h>
main (){
   char a[30] = “Hello”;
   int l;
   l = strlen (a);
   printf (“length of the string = %d”, l);
   getch ();
}

Output

length of the string = 5

The strcpy () function

  • It is for copying source string into destination string.
  • The length of the destination string >= source string.

Syntax

strcpy (Destination string, Source String);

For example,

1) char a[50];
strcpy (“Hello”,a);
o/p: error
2) char a[50];
strcpy ( a,”hello”);
o/p: a= “Hello”

Example

#include <string.h>
main (){
   char a[50], b[50];
   printf ("enter a source string");
   scanf("%s", a);
   printf("enter destination string");
   scanf("%s",b);
   strcpy ( b,a);
   printf ("copied string = %s",b);
   getch ();
}

Output

Enter a source string : Hello
Copied string = Hello

The strncpy () function

  • It copy’s ‘n’ characters of source string into destination string.

  • The length of the destination string must >= that of the source string.

Syntax

strncpy (Destination string, Source String, n);

Example

#include<string.h>
main (){
   char a[50], b[50];
   printf ("enter a string");
   gets (a);
   gets(b);
   strncpy (b,a,3);// copy first 3 char from a string
   b[3] = '\0';
   printf ("copied string = %s",b);
   getch ();
}

Output

Enter a string : Hello
Copied string = Hel
It is also used for extracting substrings;

The strcat () function

  • It combines two strings.
  • The length of the destination string must be > than the source string.

Syntax

strcat (Destination String, Source string);

Example

#include <string.h>
main(){
   char a[50] = "Hello";
   char b[20] = "Good Morning";
   clrscr ();
   strcat (a,b);
   printf("concatenated string = %s", a);
   getch ();
}

Output

Concatenated string = Hello Good Morning

The strncat () function

  • This is used for combining or concatenating n characters of one string into another.

  • The length of the destination string must be greater than the source string

  • The resultant concatenated string will be in the destination string.

Syntax

strncat (Destination String, Source string,n);

Example

#include <string.h>
main (){
   char a [30] = "Hello";
   char b [20] = "Good Morning";
   clrscr ();
   strncat (a,b,4);
   a [9] = '\0';
   printf("concatenated string = %s", a);
   getch ();
}

Output

Concatenated string = Hello Good.

The strcmp() function (String comparison)

  • This function compares 2 strings.

  • It returns the ASCII difference of the first two non – matching characters in both the strings.

Syntax

int strcmp (string1, string2);
//If the difference is equal to zero, then string1 = string2
//If the difference is positive, then string1 > string2
//If the difference is negative, then string1 < string2

Example

#include<stdio.h>
#include<string.h>
int main (){
   char a[50], b [50];
   int d;
   printf ("Enter 2 strings:");
   scanf ("%s %s", a,b);
   d = strcmp(a,b);
   if (d==0){
      printf("%s is (alphabetically) equal to %s", a,b);
   }else if (d>0){
      printf("%s is (alphabetically) greater than %s",a,b);
   }else if (d<0){
      printf("%s is (alphabetically) less than %s", a,b);
   }
}

Output

Enter 2 strings:apple ball
apple is (alphabetically) less than ball

The strncmp () function

This function is used for comparing first ‘n’ characters of 2 strings.

Syntax

strncmp ( string1, string2,2)

For example, char a[10] = “the”;

       char b[10] = “there”

       strncmp (a,b,4);

       Output − Both strings are equal

The strrev() function

  • The function is used for reversing a string.
  • The reversed string will be stored in the same string.

Syntax

strrev (string)

Example

#include<stdio.h>
main (){
   char a[50] ;
   clrscr();
   printf ("enter a string");
   gets (a);
   strrev (a);
   printf("reversed string = %s",a)
   getch ();
}

Output

enter a string Hello
reversed string = olleH

The strstr() function

  • It is used to search whether a substring is present in the main string or not.

  • It returns pointer to first occurrence of s2 in s1.

Syntax

strstr(mainsring,substring);

Example

#include<stdio.h>
void main(){
   char a[30],b[30];
   char *found;
   printf("Enter a string:\t");
   gets(a);
   printf("Enter the string to be searched for:\t");
   gets(b);
   found=strstr(a,b);
   if(found)
      printf("%s is found in %s in %d position",b,a,found-a);
   else
      printf("-1 since the string is not found");
   getch();
}

Output

Enter a string: how are you
Enter the string to be searched for: you
you is found in 8 position
raja
Updated on 09-Mar-2021 08:54:36

Advertisements