String functions in C are built-in library functions used to perform common operations on strings, such as copying, comparing, and concatenating. These functions are declared in the <string.h> header file.
- String functions simplify common string operations without requiring manual implementation.
- They are used for tasks such as finding string length, copying, concatenating, comparing, and searching strings.
Example: Copying one string to another using strcpy(), comparing two strings using strcmp(), or joining two strings using strcat().
strlen()
The strlen() function is used to determine the length of a string. It returns the number of characters in the string, excluding the null terminator '\0'.
- It counts characters until it encounters the null character '\0'.
- It is declared in the <string.h> header file.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Gfg";
// Finding and printing length of string s
printf("%lu", strlen(s));
return 0;
}
Output
3
strcpy()
The strcpy() function copies a string from the source to the destination. It copies all characters, including the null terminator '\0'.
- It is declared in the <string.h> header file.
- The destination array must have enough space to store the copied string.
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
// Copies "Hello" to dest
strcpy(dest, src);
printf("%s", dest);
return 0;
}
Output
Hello
strncpy()
The strncpy() function copies at most n characters from the source string to the destination string. It is useful when you want to limit the number of characters copied.
- It is declared in the <string.h> header file.
- If the source string is shorter than n, the remaining characters in the destination are filled with '\0'.
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello";
char dest[20];
// Copies "Hello" to dest
strncpy(dest, src, 4);
printf("%s", dest);
return 0;
}
Output
Hell
strcat()
The strcat() function appends one string to the end of another. It adds the source string after the destination string and places a null terminator at the end.
- It is declared in the <string.h> header file.
- The destination array must have enough space to store the concatenated string.
#include <stdio.h>
#include <string.h>
int main() {
char s1[30] = "Hello, ";
char s2[] = "Geeks!";
// Appends "Geeks!" to "Hello, "
strcat(s1, s2);
printf("%s", s1);
return 0;
}
Output
Hello, Geeks!
strncat()
The strncat() function appends at most n characters from the source string to the end of the destination string. It automatically adds a null terminator after the appended characters.
- It is declared in the <string.h> header file.
- The destination array must have enough space to store the resulting string.
#include <stdio.h>
#include <string.h>
int main() {
char s1[30] = "Hello, ";
char s2[] = "Geeks!";
// Appends "Geeks!" to "Hello, "
strncat(s1, s2, 4);
printf("%s", s1);
return 0;
}
Output
Hello, Geek
strcmp()
The strcmp() function compares two strings lexicographically and returns an integer indicating their relationship. It compares the strings character by character until a difference is found or both strings end.
- It is declared in the <string.h> header file.
- It returns 0 if the strings are equal, a negative value if the first string is smaller, and a positive value if the first string is greater.
#include <stdio.h>
#include <string.h>
int main() {
char s1[] = "Apple";
char s2[] = "Applet";
// Compare two strings
// and print result
int res = strcmp(s1, s2);
if (res == 0)
printf("s1 and s2 are same");
else if (res < 0)
printf("s1 is lexicographically "
"smaller than s2");
else
printf("s1 is lexicographically "
"greater than s2");
return 0;
}
Output
s1 is lexicographically smaller than s2
strncmp()
The strncmp() function compares the first n characters of two strings lexicographically. It returns an integer value based on the comparison result.
- It is declared in the <string.h> header file.
- It returns 0 if the first n characters are equal, a negative value if the first string is smaller, and a positive value if the first string is greater.
#include <stdio.h>
#include <string.h>
int main() {
char s1[] = "Apple";
char s2[] = "Applet";
// Compare two strings upto
// 4 characters and print result
int res = strncmp(s1, s2, 4);
if (res == 0)
printf("s1 and s2 are same");
else if (res < 0)
printf("s1 is lexicographically "
"smaller than s2");
else
printf("s1 is lexicographically "
"greater than s2");
return 0;
}
Output
s1 and s2 are same
strchr()
The strchr() function searches for the first occurrence of a specified character in a string. It returns a pointer to the first matching character if found.
- It is declared in the <string.h> header file.
- If the character is not found, the function returns NULL.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello, World!";
// Finding the first occurence of 'o' in string s
char *res = strchr(s, 'o');
if (res != NULL)
printf("Character found at: %ld index", res - s);
else
printf("Character not found");
return 0;
}
Output
Character found at: 4 index
strrchr()
The strrchr() function searches for the last occurrence of a specified character in a string. It returns a pointer to the last matching character if found.
- It is declared in the <string.h> header file.
- If the character is not found, the function returns NULL.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello, World!";
// Finding the last occurence of 'o' is string s
char *res = strrchr(s, 'o');
if (res != NULL)
printf("Character found at: %ld index", res - s);
else
printf("Character not found\n");
return 0;
}
Output
Character found at: 8 index
strstr()
The strstr() function searches for the first occurrence of a substring within another string. It returns a pointer to the beginning of the matched substring if found.
- It is declared in the <string.h> header file.
- If the substring is not found, the function returns NULL.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello, Geeks!";
// Find the occurence of "Geeks" in string s
char *pos = strstr(s, "Geeks");
if (pos != NULL)
printf("Found");
else
printf("Not Found");
return 0;
}
Output
Found
sprintf()
The sprintf() function formats data and stores the resulting string in a character buffer. It works like printf(), but writes the output to a string instead of displaying it on the screen.
- It is declared in the <stdio.h> header file.
- Ensure the destination buffer is large enough to hold the formatted string to avoid buffer overflow.
#include <stdio.h>
int main() {
char s[50];
int n = 10;
// Output formatted string into string bugger s
sprintf(s, "The value is %d", n);
printf("%s", s);
return 0;
}
Output
The value is 10
strtok()
The strtok() function splits a string into tokens based on specified delimiter characters. It modifies the original string by replacing delimiters with the null character '\0'.
- It is declared in the <string.h> header file.
- Successive calls to strtok() return the next token until no more tokens are found, after which it returns NULL.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello, Geeks, C!";
// Initializing tokens
char *t = strtok(s, ", ");
// Printing rest of the tokens
while (t != NULL) {
printf("%s\n", t);
t = strtok(NULL, ", ");
}
return 0;
}
Output
Hello Geeks C!
Most commonly used string functions in the C
The below table lists some of the most commonly used string functions in the C language:
Function | Description | Syntax |
|---|---|---|
Find the length of a string excluding '\0' NULL character. | strlen(str); | |
Copies a string from the source to the destination. | strcpy(dest, src); | |
Copies n characters from source to the destination. | strncpy( dest, src, n ); | |
Concatenate one string to the end of another. | strcat(dest, src); | |
Concatenate n characters from the string pointed to by src to the end of the string pointed to by dest. | strncat(dest, src, n); | |
Compares these two strings lexicographically. | strcmp(s1, s2); | |
strncmp() | Compares first n characters from the two strings lexicographically. | strncmp(s1, s2, n); |
Find the first occurrence of a character in a string. | strchr(s, c); | |
Find the last occurrence of a character in a string. | strchr(s, ch); | |
First occurrence of a substring in another string. | strstr(s, subS); | |
Format a string and store it in a string buffer. | sprintf(s, format, ...); | |
Split a string into tokens based on specified delimiters. | strtok(s, delim); |