After reading this string dealing functions topic, you will understand its theory and examples also you will able to use available different string functions in C programming.
In C programming various Library functions are available for handling string data and we need only to include <string.h> header file where all these string handling functions are defined. These string handling functions are:
1 strcat():- to concatenates ( or connect together) two strings. The general form is:
strcat(string1,string2);
The example below will show how strcat() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="Dennis",string2[25]="Ritchie"; strcat(string1,string2); printf("%s",string1); getch(); }
Output
DennisRitchie
2 strlen():- to find length of string including blank space. The general form is:
strlen(string1);
The example below will show how strlen() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="Dennis Ritchie"; printf("%d",strlen(string1)); getch(); }
Output
14
3 strcpy():- to copy one string over another. The general form is:
strcpy(string1,string2);
The example below will show how strcpy() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="Dennis"; char string2[25]="Ritchie"; strcpy(string1,string2); printf("%s\n %s",string1,string2); getch(); }
Output
Ritchie Ritchie
4 strcmp():- to compare two string and this function is case-sensitive. The general form is:
strcmp(string1,string2);
The example below will show how strcmp() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="DENNIS"; char string2[25]="Dennis"; if(strcmp(string1,string2)==0) printf("strings are equal"); else printf("strings are not equal"); getch(); }
Output
strings are not equal
5 strcmpi():- to compare two string and this function is not case-sensitive. The general form is:
strcmpi(string1,string2);
The example below will show how strcmpi() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="DENNIS"; char string2[25]="Dennis"; if(strcmpi(string1,string2)==0) printf("strings are equal"); else printf("strings are not equal"); getch(); }
Output
strings are equal
6 strlwr():- to convert string to lower case. The general form is:
strlwr(string1);
The example below will show how strlwr() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="DENNIS",string2[25]; strlwr(string1); printf("Lower case :- %s",string1); getch(); }
Output
Lower case :- dennis
7 strupr():- to convert string to upper case. The general form is:
strlupr(string1);
The example below will show how strupr() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="dennis",string2[25]; strupr(string1); printf("Upper case :- %s",string1); getch(); }
Output
Upper case :- DENNIS
8 strrev():- to reverse the string. The general form is:
strrev(string1);
The example below will show how strrev() function used in C programming.
#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { char string1[25]="dennis",string2[25]; strrev(string1); printf("Reverse form :- %s",string1); getch(); }
Output
Reverse form :- sinned