C string.h Header
String manipulation and handling functions
🔤 What is string.h?
The string.h header provides functions for manipulating strings and memory blocks. It includes functions for copying, comparing, searching, and modifying strings efficiently in C programs.
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, " ");
strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
Output:
Hello World
Key string.h Functions
strlen()
Get length of string
int len = strlen("Hello");
strcpy()
Copy one string to another
strcpy(dest, source);
strcat()
Concatenate two strings
strcat(str1, str2);
strcmp()
Compare two strings
int result = strcmp(str1, str2);
🔹 String Operations Example
C provides a rich set of string manipulation functions through the string.h library for common text processing tasks. Functions like strlen() calculate string length, strcpy() and strncpy() copy strings safely, while strcat() and strncat() concatenate strings. The strcmp() function compares strings lexicographically, returning zero for equal strings, negative for less-than, and positive for greater-than relationships. For example, char dest[50]; strcpy(dest, "Hello"); safely copies "Hello" into the destination buffer. Always ensure destination buffers are large enough to prevent buffer overflows, and consider using safer alternatives like strncpy() that limit the number of characters copied.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Programming";
char str2[50] = "Language";
char str3[100];
// Get string length
printf("Length of '%s': %lu\n", str1, strlen(str1));
// Copy string
strcpy(str3, str1);
printf("Copied string: %s\n", str3);
// Concatenate strings
strcat(str3, " ");
strcat(str3, str2);
printf("Concatenated: %s\n", str3);
// Compare strings
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are different\n");
}
return 0;
}
Output:
Length of 'Programming': 11 Copied string: Programming Concatenated: Programming Language Strings are different
🔹 String Search Functions
String search functions in C enable locating characters and substrings within larger strings using the string.h library. The strchr() and strrchr() functions find the first and last occurrence of a character respectively, returning a pointer to the found position or NULL if not found. For substring searches, strstr() locates the first occurrence of a substring within a string. For example, char *pos = strchr("hello", 'l'); returns a pointer to the first 'l' in "hello". These functions are essential for parsing text, validating input, and implementing search features in text-processing applications efficiently.
#include <stdio.h>
#include <string.h>
int main() {
char text[] = "Hello World Programming";
char *result;
// Find first occurrence of character
result = strchr(text, 'o');
if (result != NULL) {
printf("First 'o' found at position: %ld\n", result - text);
}
// Find substring
result = strstr(text, "World");
if (result != NULL) {
printf("'World' found at position: %ld\n", result - text);
printf("Substring from 'World': %s\n", result);
}
return 0;
}
Output:
First 'o' found at position: 4 'World' found at position: 6 Substring from 'World': World Programming