What is Standard Library String Function in C Language with Free Notes

Table of Contents

Introduction

The C Library supplies several string handling functions. ANSI C uses ‘string.h’ header file to provide the prototype. Some most commonly used strings Library functions are give below:

String FunctionUSE
strlenFinds he Length of the String.
strcatAppends one string at the end of another string.
strcpyCopies one string to another string.
strcmpCompares two strings.
strrevReverse two Strings.
struprConverts string into uppercase.
strlwrConverts string into lowercase.

These functions can be discussed in detail as below:

Free E-Books

1. Strlen() – Library

The arguments of this functions is a string and it calculates its length i.e. it returns the number of characters in the string except the null character ‘\0’.

Example: Demonstration of strlen() function

#include<stdio,h>
#include<conio.h>
#include<string.h>
void main()    
        {
         char str[80];
         clrscr();
         printf("\n Enter a String");
         gets(str);
         puts(str);
         printf("\n The length of the string = %d", strlen(str));
         getch();
        }

Output

Enter a string
Computonix club
Computonix club
The length of the string = 15 

2. strcat () – Library

It appends a source string to the end of destination string so that the length of the resulting string is equal to total of both the strings.

It takes two arguments, of which first is a string variable and second can be a string constant or a variable.

Free Hand-Written Notes

Example: Demonstration of strcat() function

#include<stdio,h>
#include<conio.h>
#include<string.h>
void main()    
        {
         char str[35], blank[]= " ";
         char s1[]= "Pawan",s2[] = "Preet",s3[] ="Singh";
         clrscr();
         strcat(result, s1);
         strcat(result, blank);
         strcat(result, s2);
         strcat(result, blank);
         strcat(result, s3);
         printf("\n The Resultant String is...");
         puts(result);
          getch();
        }

Output

The Resultant String is...
Pawan Preet Singh

3. Strcpy() – Library

  • This Function copies one String to another.
  • It requires two arguments, of which first is a string variable and second can be a string constant or a variable.
  • It copies the characters of the second arguments to the first one.
  • The general form of this functions is given below: –
strcpy(dest, source);
  • It copies source string to dest string. The copying action stops after the null character ‘\0’ has moved from source to dest.

Example: Demonstration of strcpy() function

#include<stdio,h>
#include<conio.h>
#include<string.h>
void main()    
        {
         char str[20], dest[20];
         clrscr();
         printf("\n Enter a String");
         gets(source);
         strcpy(dest, source);
         printf("\n The entered string is");
         puts(source);
         printf("\n The copied String");
         puts(dest);
          getch();
        }

Output

Enter a String Pawan
The Entered String is Pawan 
The copied String is Pawan

4. Strcmp() – Library

  • The function compares two strings s1 and s2.
  • The comparison begins with the first character of both strings and continues with subsequent characters until the end of the strings is reached or the compared characters differ from each other.
  • Strcmp() returns a value less than zero if s1<s2, zero if s1 = s2. It returns value more than zero if s1>s2. (In dictionary order).
Play Quiz Gain Knowledge

Example: Demonstration of strcmp() function

#include<stdio,h>
#include<conio.h>
#include<string.h>
void main()    
        {
         char s1[20], s2[20];
         clrscr();
         printf("\n Enter the first String");
         gets(s1);
         printf("\n Enter the second String");
         puts(s2);
            if(strcmp(s1, s2) == 0)
                 printf("\n The strings are equal");
              else 
                 printf("\n The strings are not equal");
              getch();
        }

Output

Enter first string is Computer 
Enter the second string Compute
The strings are not equal

5. Strrev()

  • This function reverse the string i.e. the last character becomes the first one, the second last becomes the second one and so on.
  • It takes single arguments i.e. string variable.

Example: Demonstration of strrev() function,

#include<stdio,h>
#include<conio.h>
#include<string.h>
void main()    
        {
         char str[20];
         clrscr();
         printf("\n Enter the String");
         gets(str);
         printf("\n String enterd is %s", str);
         printf("\n String after reverse is %s" , strrev (str));
           getch();
        }

Output

Enter the String YELEAY
String entered is YELEAY
String after reverse is YAELEY

6. Strupr()

  • This function is used to convert the characters of a string into uppercase letters.
  • It takes single argument i.e. string constant or a string variable.

Example: Demonstration of strupr() function

#include<stdio,h>
#include<conio.h>
#include<string.h>
void main()    
        {
         char str[20];
         clrscr();
         printf("\n Enter the String");
         gets(str);
         printf("\n String enterd is %s", str);
         printf("\n In uppercase letters entered string is %s" , strupr(str));
           getch();
        }

Output

Enter a string computer 
Entered string is computer 
In uppercase letters entered string is COMPUTER

7. Strlwr()

  • This function is used to convert the characters of a strings into lowercase letters.
  • It also takes single arguments.

Example: Demonstration of strwlr() function

#include<stdio,h>
#include<conio.h>
#include<string.h>
void main()    
        {
         char str[20];
         clrscr();
         printf("\n Enter the String");
         gets(str);
         printf("\n String entered is %s", str);
         printf("\n In lowercase letters entered string is %s" , strlwr(str));
           getch();
        }

Output

Enter a String BELA
String entered is BELA
In lowercase letters entered string is bela

Leave a Comment

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

Scroll to Top