String functions in c language with examples | strcpy, strcat, strcmp, strrev, strlen in C
We need to include string.h header file in a program to use string functions. Different string functions in c language are:
1. strcpy()
strcpy() is one of the most popular String functions in c language . This function is used store a value in a string variable
Syntax :
strcpy(Str2,Str1);
Str2 is the string variable to store the value.
Str1 is the string value to be stored in string variable Str2.
We can’t use assignment operator = to assign a value to string variable. So we need to use strcpy() function to do that.
char name[20];
strcpy(name,”Amit”);
Program to demonstrate the use of strcpy() function in C. |
#include<stdio.h> #include<string.h> int main() { char name[20]; strcpy(name,”Rahul”); printf("name=%s",name); return(0); } |
Output |
name=Rahul
|
2. strcat()
strcat() function is used to combine values of two string variables .
Syntax
strcat(Str2,Str1);
Str2 is the string variable whose value will be combined with another string value.
Str1 is the string value to be combined with string variable Str2.
Program to demonstrate the use of strcat() function in C. |
#include<stdio.h> #include<string.h> int main() { char name[30]=”Rahul”; strcat(name,” Singh”); printf("name=%s",name); return(0); } |
Output |
name=RahulSingh
|
Description of above program
In above program, string variable studentname contains value “Amit”. After applying strcat() function, its value will be concatenated with “ Singh” and the final value of string variable studentname would become Amit Singh
3. strlen()
strlen() function is used to count and return number of characters in a string value.
Program to demonstrate the use of strlen() function in C. |
#include<stdio.h> #include<string.h> int main() { char name[30]="Rahul"; int num; num=strlen(name); printf("\nNumber of characters=%d",num); return(0); } |
Output |
Number of characters=5
|
Description of above program
In above program, string variable studentname contains value “Amit”. strlen() function finds number of characters in the value “Amit” in string variable studentname that contains 4 characters. So strlen() would return 4.
4. strrev()
strrev() function is used to reverse the characters stored in a string value.
Program to demonstrate the use of strrev() function in C. |
#include<stdio.h> #include<string.h> int main() { char name[20]=”Rahul”; strrev(name); printf("name=%s",name); return(0); } |
Output |
name=luhaR |
Description of above program
In above program, string variable name contains “Rahul”. strrev() reverses the value of name to make it luhaR.
5. strcmp()
strcmp() function is used to compare two string values. 0 is returned if the values of strings being compared are same.
Program to demonstrate the use of strcmp() function in C. |
#include<stdio.h> #include<string.h> int main() { char name[35]; strcpy(name,”Sumit”); if(strcmp(studentname,”Amit”)==0) cout<<”\nWelcome Amit”; else cout<<”Wrong User”; return(0); } |
Output |
Wrong User |
Description of above program
In this program, string variable name contains “Sumit”. String variable name has been compared with the string value Amit. Value of name i.e. sumit doesn’t match Amit so the output would be wrong user.
6. strcmpi()
strcmpi() function is used to compare string values but capital letters and small letters are considered same.
Program to demonstrate the use of strcmpi() function in C |
#include<stdio.h> #include<string.h> int main() { char n1[30],n2[30]; printf("Enter a string value="); gets(n1); printf("Enter another string value="); gets(2); if(strcmpi(n1,n2)==0) printf("Same"); else printf("Not same"); return(0); } |
Output |
Enter a string value=Aman Enter another string value=AMAN Same |
Description of above program
In this program, string variable n1 contains value “Aman” and n2 contains “AMAN”. n1 and n2 contain same values with different cases. They are considered same so message “Same” gets displayed .
7. strlwr()
strlwr() function converts the uppercase letters to lowercase letters in a string value.
Program to demonstrate the use of strlwr() function in C |
#include<stdio.h> #include<string.h> int main() { char sname[30]=”SUMIT”; strlwr(sname); printf("\nsname=%s",sname); return(0); } |
Output |
sname=sumit
|
Description of above program
In this program, string variable sname contains “Sumit”. strlwr() function converts the value into small case i.e. sumit.
8. strupr()
strupr() function converts the small case letters to capital letters in a string value.
Program to demonstrate the use of strupr() function in C |
#include<stdio.h> #include<string.h> int main() { char sname[30]=”sumit”; strupr(sname); printf(“\nsname=%s”,sname); return(0); } |
Output |
sname=SUMIT |
Description of above program
In this program, string variable sname contains “sumit”. strupr() function converts the value into capital case i.e. “SUMIT”.
Write C program for following:
- Program to read a name. If the name is “Raman”, message “Hi” should be displayed otherwise “Bye” should be displayed.
- Program to read name and password of a user. If name is “Ramesh” and password is “Sharma”. Multiplication of two numbers should be displayed otherwise division of those numbers should be displayed.