C String Functions
There are many important string functions defined in
"string.h" library.
(1) :- C String Length: strlen() function
The strlen() function returns the length of the given
string. It doesn't count null character '\0'.
Program 1 :-
#include<stdio.h>
#include <string.h>
void main()
char str[10]={'H', 'E', 'L', 'L', 'O', '\0'};
printf("Length of string is: %d",strlen(str));
Output:
Length of string is: 5
(2):- C Copy String: strcpy() function
The strcpy(destination, source) function copies the source string in
destination.
Program 2 :-
#include<stdio.h>
#include <string.h>
void main(){
char str1[20]={'p', 'r', 'o', 'g', 'r', 'a', 'm', '\0'};
char str2[20];
strcpy(str2,str1);
printf("Value of second string is: %s",str2);
Output:
Value of second string is:program
(3):- C String Concatenation: strcat() function
The strcat(first_string, second_string) function concatenates two strings
and result is returned to first_string.
Program 3 :-
#include<stdio.h>
#include <string.h>
void main()
char str1[20]={'h', 'e', 'l', 'l', 'o', '\0'};
char str2[20]={'c', '\0'};
strcat(str1,str2);
printf("Value of first string is: %s",str1);
Output:
Value of first string is: helloc
(4):- C Compare String: strcmp() function
The strcmp(first_string, second_string) function compares two string
and returns 0 if both strings are equal.
Here, we are using gets() function which reads string from the console.
Program 4:-
#include<stdio.h>
#include <string.h>
Void main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
}
Output:
Enter 1st string: hello
Enter 2nd string: hello
Strings are equal
(5):- C Reverse String: strrev() function
The strrev(string) function returns reverse of the given string. Let's see
a simple example of strrev() function.
Program 5 :-
#include<stdio.h>
#include <string.h>
void main()
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
}
Output:
Enter string: program
String is: program
Reverse String is: margorp
(5):- C String Lowercase: strlwr() function
The strlwr(string) function returns string characters in lowercase. Let's
see a simple example of strlwr() function.
Program 5 :-
#include<stdio.h>
#include <string.h>
Void main()
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nLower String is: %s",strlwr(str));
}
Output:
Enter string: PROGRAM
String is: PROGRAM
Lower String is: program
(6) :- C String uppercase: strupr() function
The strupr(string) function returns string characters in uppercase. Let's
see a simple example of strupr() function.
Program 6:-
#include<stdio.h>
#include <string.h>
void main()
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
}
Output:
Enter string: program
String is: program
Upper String is: PROGRAM