This post contains a list of predefined string functions defined in string.h
with examples. You can learn more about C string from our C course.
The functions present in the string.h
header are:
Function | Use |
---|---|
strlen | calculates the length of string |
strcat | Appends one string at the end of another |
strncat | Appends first n characters of a string at the end of another |
strcpy | Copies a string into another |
strncpy | Copies first n characters of one string into another |
strcmp | Compares two strings |
strncmp | Compares first n characters of two strings |
strchr | Finds the first occurrence of a given character in a string |
strrchr | Finds the last occurrence of a given character in a string |
strstr | Finds the first occurrence of a given string in another string |
strcspn | Calculates the initial length of a string, not containing any character of the other string |
strspn | Calculates the initial length of a string, containing the only characters of the other string |
strpbrk | Finds the first occurrence of any of the characters of a string in another string |
strtok | splits a string into token |
Examples
strlen
strlen(s1) calculates the length of string s1.
#include <stdio.h>
#include <string.h>
int main()
{
char name[ ]= "Hello";
int len1, len2;
len1 = strlen(name);
len2 = strlen("Hello World");
printf("length of %s = %d\n", name, len1);
printf("length of %s = %d\n", "Hello World", len2);
return 0;
}
Output
length of Hello = 5
length of Hello World = 11
strlen doesn't count '\0' while calculating the length of a string.
strcat
strcat(s1, s2) concatenates(joins) the second string s2 to the first string s1.
#include <stdio.h>
#include <string.h>
int main()
{
char s2[ ]= "World";
char s1[20]= "Hello";
strcat(s1, s2);
printf("Source string = %s\n", s2);
printf("Target string = %s\n", s1);
return 0;
}
Output
Source string = World
Target string = HelloWorld
strncat
strncat(s1, s2, n) concatenates(joins) the first ‘n’ characters of the second string s2 to the first string s1.
#include <stdio.h>
#include <string.h>
int main()
{
char s2[ ]= "World";
char s1[20]= "Hello";
strncat(s1, s2, 2);
printf("Source string = %s\n", s2);
printf("Target string = %s\n", s1);
return 0;
}
Source string = World
Target string = HelloWo
strcpy
strcpy(s1, s2) copies the second string s2 to the first string s1.
#include <string.h>
#include <stdio.h>
int main()
{
char s2[ ]= "Hello";
char s1[10];
strcpy(s1, s2);
printf("Source string = %s\n", s2);
printf("Target string = %s\n", s1);
return 0;
}
Output
Source string = Hello
Target string = Hello
strncpy
strncpy(s1, s2, n) copies the first ‘n’ characters of the second string s2 to the first string s1.
#include <string.h>
#include <stdio.h>
int main()
{
char s2[ ]= "Hello";
char s1[10];
strncpy(s1, s2, 2);
s1[2] = '\0'; /* null character manually added */
printf("Source string = %s\n", s2);
printf("Target string = %s\n", s1);
return 0;
}
Output
Source string = Hello
Target string = He
Please note that we have added the null character (‘\0’) manually.
strcmp
strcmp(s1, s2) compares two strings and finds out whether they are same or different. It compares the two strings character by character till there is a mismatch. If the two strings are identical, it returns a 0. If not, then it returns the difference between the ASCII values of the first non-matching pair of characters.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hello";
char s2[ ]= "World";
int i, j;
i = strcmp(s1, "Hello");
j = strcmp(s1, s2);
printf("%d \n %d\n", i, j);
return 0;
}
0
-15
The difference between the ASCII values of H(72) and W(87) is -15.
strncmp
strncmp(s1, s2, n) compares the first ‘n’ characters of s1 and s2.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hello";
char s2[ ]= "World";
int i, j;
i = strncmp(s1, "He BlogsDope", 2);
printf("%d\n", i);
return 0;
}
Output
0
strchr
strchr(s1, c) returns a pointer to the first occurrence of the character c in the string s1 and returns NULL if the character c is not found in the string s1.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hello Blogsdope";
char c = 'B';
char *p;
p = strchr(s1, c);
printf("%s\n", p);
return 0;
}
Output
Blogsdope
strrchr
strrchr(s1, c) returns a pointer to the last occurrence of the character c in the string s1 and returns NULL if the character c is not found in the string s1.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hello Blogsdope";
char c = 'o';
char *p;
p = strrchr(s1, c);
printf("%s\n", p);
return 0;
}
Output
ope
strstr
strstr(s1, s2) finds the first occurrence of the string s2 in the string s1.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "HelloBlogsdope";
char s2[ ] = "Blog";
char *p;
p = strstr(s1, s2);
printf("%s\n", p);
return 0;
}
Output
BlogsDope
strcspn
strcspn(s1, s2) returns the length of the initial part of the string s1 not containing any of the characters of the string s2.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "HelloBlogsdope";
char s2[ ] = "lo";
int i = strcspn(s1, s2);
printf("The first character is matched at %d\n", i+1);
return 0;
}
Output
The first character is matched at 3
strspn
strspn(s1, s2) returns the length of the initial part of the string s1 only containing the characters of the string s2.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "123abc";
char s2[ ] = "123456790";
int i = strspn(s1, s2);
printf("%d\n", i);
return 0;
}
Output
3
strpbrk
strpbrk(s1, s2) returns the first occurrence of any of the characters of the string s2 in the string s1.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hey 123 Blogsdope";
char s2[ ] = "123456790";
char *p;
p = strpbrk(s1, s2);
printf("%s\n", p);
return 0;
}
Output
123 Blogsdope
strtok
strtok(s1, s2) finds s2 in s1 and returns a pointer to it and returns NULL if not found.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[ ]= "Hey,123,Blogsdope";
char *p;
p = strtok(s1, ",");
while(p != NULL)
{
printf("%s\n", p);
p = strtok(NULL, ",");
}
return 0;
}
Output
Hey
123
Blogsdope