C - String Library Function
There are several built-in functions available for working with strings. These functions are declared in the <string.h> header file.
strlen():
Returns the length of a string, excluding the null character.
#include <string.h>
// ...
char str[] = "Hello";
int length = strlen(str); // length is 5
strcpy():
Copies one string to another.
#include <string.h>
// ...
char source[] = "Hello";
char destination[10];
strcpy(destination, source); // destination now contains "Hello"
strcat():
Concatenates (appends) one string to the end of another.
#include <string.h>
// ...
char str1[] = "Hello"
char str2[] = " World";
strcat(str1, str2); // str1 now contains "Hello World"
strcmp():
Compares two strings lexicographically.
#include <string.h>
// ...
char str1[] = "Hello"
char str2[] = "World";
int result = strcmp(str1, str2); // result is negative (-1)
strchr():
Searches for the first occurrence of a character in a string and returns a pointer to that character.
#include <string.h>
// ...
char str[] = "Hello";
char* ptr = strchr(str, 'l'); // ptr points to the first 'l' in str
strstr():
Searches for the first occurrence of a substring in a string and returns a pointer to that substring.
#include <string.h>
// ...
char str[] = "Hello World";
char* ptr = strstr(str, "World"); // ptr points to the substring "World" in str