STRING STANDARD FUNCTIONS IN C:
- strlen() - Determines the length of a string.
Eg: char name[]="antony";
int length;
length=strlen(name);
printf("%d",length);
Output:
6
- strcpy(s1,s2) - copies the content of s2 to s1.
Eg: char source[]="hai";
char destination[3];
strcpy(destination,source);
printf("source=%s destination=%s",source,destination);
Output:
source=hai destination=hai
- strncpy(s1,s2,n) - copies 1st n characters from s2 to s1.
Eg: char source[]="hai";
char destination[3];
strcpy(destination,source,2);
printf("source=%s destination=%s",source,destination);
Output:
source=hai destination=ha
- strcmp(s1,s2) - compares two strings. Case sensitive.
return value result
zero - s1 = s2
negative - s1 < s2
positive - s1 > s2
Eg: char s1[]="hai";
char s2[]="hai";
printf("%d\n",strcmp(s1,s2));
printf("%d\n",strcmp(s1,"hello"));
printf("%d\n",strcmp("hello",s1));
Output:
0
-1
1
- stricmp(s1,s2) - similar to strcmp(), but it is not case sensitive.
- strncmp(s1,s2,n) - similar to strcmp(), but comparison is done only till n characters
- strnicmp(s1,s2,n) - compares n characters and it is not case sensitive.
- strlwr(s) - convert s to lower case.
Eg: char s[]="ABCDE";
printf("%s",strlwr(s));
Output:
abcde
- strupr(s) - converts s to upper case.
- strdup(s) - duplicates the given string at the allocated memory which is pointed by a pointer variable.
Eg: char text1[]="hai",*text2;
text2=strdup(text1);
printf("original=%s duplicate=%s",text1,text2);
Output:
original=hai duplicate=hai
- strchr(string,ch) - returns the pointer to the first occurence of a given character in the string.
Eg: char s[]="have a nice day!";
char *c;
c=strchr(s,'a');
if(c)
printf("%c is found",c);
else
printf("%c is not found",c);
Output:
a is found
- strrchr(string,ch) - similar to strchr(). strchr() searches for the occurence of characters from the beginning of the string but strrchr() searches for the occurence of characters from the end.
- strstr(string1,string2) - finds string2 in string1 and returns the pointer to the location from where the second string starts in the first string.
Eg: char str1[]="welcome to my blog!!!";
char str2[]="welcome";
char *c;
c=strstr(str1,str2);
if(c)
printf("%s is found",str2);
else
printf("%s is not found",str2);
Output:
welcome is found
- strcat(str1,str2) - appends str2 at the end of str1 and the resultant string is stored in str1.
Eg: char s1[]="hai", s2[]="hello";
strcat(s1,s2);
printf("%s",s1);
Output:
haihello
- strncat(str1,str2,n) - similar to strcat. 1st n characters of str2 are appended to the end of str1.
- strrev(string) - reverses the string.
Eg: char a[]="hai";
puts(strrev(a));
Output:
iah
- strset(string,symbol) - replaces every character of the string with the symbol.
Eg: char s[]="hai";
strset(s,"*");
printf("%s",s);
Output:
***
- strnset(string,symbol,n) - replaces 1st n characters of the string with the symbol.
- strspn(str1,str2) - returns the no. of characters matched upto the 1st mismatch character.
Eg: char str1[]="hawaii", str2[]="hang";
int length;
length=strspn(str1,str2);
printf("two strings are equal upto %d characters",length);
Output:
two strings are equal upto 2 characters
- strpbrk(str1,ch) - searches the 1st occurence of a character in the given string and then it displays the string starting from that character.
Eg: char s[]="hello world!!!";
char *ptr;
ptr=strpbrk(s,'o');
puts(ptr);
Output:
orld!!!