String function : The header file <string.h> is used .the string header file contain the code of the string function.
1) strlen() : this function is used to count the lenth of the string.
Suntax : strlrn(veriable_name);
Emxaple :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”;
printf(“lenth of name = %s”,strlen(name));
}
out put : lenth of name = 5.
So, you can find that the string lenth is 5.
2)strcpy() : this function is used to copy one string to another string.
Syntex : strcpy(first_string,second_string);
Now ,here the second_string is copy to the first_string because it work with right to left.
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”,a[7];
strcpy(a,name);
printf(“a = %s”,a);
}
out put : a = hello
3) strcmp() : this function is used to compare the two string.the two string equal or not.
Syntex : strcmp(first_string,second_string);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”,a[]=”hii”
if(strcmp(name,a)==0)
{
printf(“they are equal”);
}
else
{
printf(“they are not equal”);
}
}
out put : they are not equal.
4) strcat() : the strcat function is used to join the two one string to another.it takes two string as arguments ; the characters of the second string will be appended to the first string.
Syntax : strcat(first_string,second_string);
Example :
#include<stdio.h>
#Include<string.h>
void main()
{
char name[]=”hello”,a[]=” hii”;
printf(“%s”,strcat(name,a));
}
out put : hello hii.
5) strlwr() :this function is convert the upper character to lower character, it take one string and convert into lower string.
Syntax : strlwr(string_name);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”HEllo”;
printf(“after lower = %s”,strlwr(name));
}
out put : after lower = hello.
6) strrev() : it is used to reserve the string.it take the string and convert into the reserve.
Syntax : strrev(string_name);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”;
printf(“after reserve = %s”,name);
}
out put : after reserve = olleh.
7)strspn() : the strspn function return the possition of the string.where the first string mismatch with second string.
Syntax : strspn(first_string,second_string);
Example :
#include<stdio.h>
#Include<string.h>
void main()
{
char name[]=”jashihoney”,a[]=”joshividhi”;
printf(“after %d character there is no match”,strspn(name,a));
}
out put : after 5 characters there no mstch.
8) strncpy() : the strncpy function same as strcpy. It copies characters of second string.first string until either null character or the specified length have been copied whichever occurs first and return result in first string.
Syntax : strncpy(first_string,second_string,10);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char a[]=”abcde”,b[]=”fghij”;
printf(“%s”,strncpy(a,b,3));
}
out put : fghde.
9) stricmp : this functio is same as a strcmp but in stricmp it ingnore ( ( the upper and lower case ) and compare the two string.
Syntax : srticmp(fist_string,second_string);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char a[]=”HELLO”,b[]=”hello”;
if(stricmp(a,b)==0)
{
printf(“two string are equal”);
}
else
{
printf(“two string are not equal”);
}
}
out put ; two string are not equal.
10) strchr () : the strchr function takes two arguments(the string and the character whose adress is to be specified) and returns the adsress of first occurence of the character in the given string.
where str is string and c is a character and cp is character pointer
11) strset () : the strset functio replace the string with the given character. It take two arguments the string and character.
Syntax : strset(first_string,ch);
where string first will be replaces by characters ch.
12 ) strncat() : the strncat function is the same as strcat,except that it appends upt specified length.
Syntax : strncat(firat_string,second_string,10);
where 10 character of the second_string string is added into first_string string.
13) strstr() : the strstr function take two arguments address of the string and secor string as inputs.and returns the address from where the second string start in the string.
Syntax :
cp = strstr (first,second);
where first and second are two string. Cp is character pointer.
Following is a list of the common string managing functions in C.
1) strlen() : this function is used to count the lenth of the string.
Suntax : strlrn(veriable_name);
Emxaple :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”;
printf(“lenth of name = %s”,strlen(name));
}
out put : lenth of name = 5.
So, you can find that the string lenth is 5.
2)strcpy() : this function is used to copy one string to another string.
Syntex : strcpy(first_string,second_string);
Now ,here the second_string is copy to the first_string because it work with right to left.
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”,a[7];
strcpy(a,name);
printf(“a = %s”,a);
}
out put : a = hello
3) strcmp() : this function is used to compare the two string.the two string equal or not.
Syntex : strcmp(first_string,second_string);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”,a[]=”hii”
if(strcmp(name,a)==0)
{
printf(“they are equal”);
}
else
{
printf(“they are not equal”);
}
}
out put : they are not equal.
4) strcat() : the strcat function is used to join the two one string to another.it takes two string as arguments ; the characters of the second string will be appended to the first string.
Syntax : strcat(first_string,second_string);
Example :
#include<stdio.h>
#Include<string.h>
void main()
{
char name[]=”hello”,a[]=” hii”;
printf(“%s”,strcat(name,a));
}
out put : hello hii.
5) strlwr() :this function is convert the upper character to lower character, it take one string and convert into lower string.
Syntax : strlwr(string_name);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”HEllo”;
printf(“after lower = %s”,strlwr(name));
}
out put : after lower = hello.
6) strrev() : it is used to reserve the string.it take the string and convert into the reserve.
Syntax : strrev(string_name);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char name[]=”hello”;
printf(“after reserve = %s”,name);
}
out put : after reserve = olleh.
7)strspn() : the strspn function return the possition of the string.where the first string mismatch with second string.
Syntax : strspn(first_string,second_string);
Example :
#include<stdio.h>
#Include<string.h>
void main()
{
char name[]=”jashihoney”,a[]=”joshividhi”;
printf(“after %d character there is no match”,strspn(name,a));
}
out put : after 5 characters there no mstch.
8) strncpy() : the strncpy function same as strcpy. It copies characters of second string.first string until either null character or the specified length have been copied whichever occurs first and return result in first string.
Syntax : strncpy(first_string,second_string,10);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char a[]=”abcde”,b[]=”fghij”;
printf(“%s”,strncpy(a,b,3));
}
out put : fghde.
9) stricmp : this functio is same as a strcmp but in stricmp it ingnore ( ( the upper and lower case ) and compare the two string.
Syntax : srticmp(fist_string,second_string);
Example :
#include<stdio.h>
#include<string.h>
void main()
{
char a[]=”HELLO”,b[]=”hello”;
if(stricmp(a,b)==0)
{
printf(“two string are equal”);
}
else
{
printf(“two string are not equal”);
}
}
out put ; two string are not equal.
10) strchr () : the strchr function takes two arguments(the string and the character whose adress is to be specified) and returns the adsress of first occurence of the character in the given string.
where str is string and c is a character and cp is character pointer
11) strset () : the strset functio replace the string with the given character. It take two arguments the string and character.
Syntax : strset(first_string,ch);
where string first will be replaces by characters ch.
12 ) strncat() : the strncat function is the same as strcat,except that it appends upt specified length.
Syntax : strncat(firat_string,second_string,10);
where 10 character of the second_string string is added into first_string string.
13) strstr() : the strstr function take two arguments address of the string and secor string as inputs.and returns the address from where the second string start in the string.
Syntax :
cp = strstr (first,second);
where first and second are two string. Cp is character pointer.
0 Comments