All the string handling library functions in C are present in string.h header file.
Using string library functions we can perform following operations on strings:
- Find the string length.
- Copy string.
- Concatenate(join) string.
- Comparison of string.
- Reverse a string.
- Covert string to lowercase.
- Convert string to uppercase.
Following are the important string handling library functions in C:
strlen(string_name) : returns the length of string.strcpy(destination, source) : copies the contents of source string to destination string.
strcat(first_string, second_string) : joins first string with second string. The result of the string is stored in first string.
strcmp(first_string, second_string) : compares the first string with second string. If both strings are same, it returns 0.
strrev(string) : returns the reverse of string.
strlwr(string) : returns string characters in lowercase.
strupr(string) : returns string characters in uppercase.
Example program: String handling functions in C
#include<stdio.h>#include<string.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
int l;
printf("Enter string 1: ");
scanf("%s",s1);
printf("Enter string 2: ");
scanf("%s",s2);
l=strlen(s1);
printf("Length of string 1: %d\n",l);
l=strlen(s2);
printf("Length of string 2: %d\n",l);
printf("String comparison: ");
if(strcmp(s1,s2)==0)
printf("Strings are equal\n");
else
printf("Strings are not equal\n");
printf("Upper case of string 1: %s\n",strupr(s1));
printf("Loweer case of string 2: %s\n",strlwr(s2));
printf("String concatenation:\n");
strcat(s1,s2);
printf(" string 1: %s\n",s1);
strcpy(s1,s2);
printf("After copy strings are\n");
printf(" string 1: %s\n",s1);
printf("string 2: %s\n",s2);
printf("Reverse of string 2: %s\n",strrev(s2));
getch();
}
Output 1:
Enter string 1: myEnter string 2: COLLEGE
Length of string 1: 2
Length of string 2: 7
String comparison: Strings are not equal
Upper case of string 1: MY
Lower case of string 2: college
String concatenation:
string 1: MYcollege
After copy strings are
string 1: college
string 2: college
Reverse of string 2: egelloc
Output 2:
Enter string 1: learningEnter string 2: learning
Length of string 1: 8
Length of string 2: 8
String comparison: Strings are equal
Upper case of string 1: LEARNING
Lower case of string 2: learning
String concatenation:
string 1: LEARNINGlearning
After copy strings are
string 1: learning
string 2: learning
Reverse of string 2: gninrael
QUIZ:
1] All string handling functions in C are present in header file.- math.h
- string.h
- ctype.h
- conio.h
- copy()
- strcopy()
- strcpy()
- None