<string.h> Library: String ManipulationIn C, strings are not objects like they are in Java or Python. Instead, a string is simply an array of characters that ends with a special "null terminator" character ('\0').
Because strings are just raw arrays in memory, you cannot manipulate them using standard operators (e.g., string1 + string2 or string1 == string2 will not work). To work with strings effectively, C provides the <string.h> standard library.
To use any of these functions, you must include the header at the top of your file:
#include <string.h>
strlen()The strlen() function calculates the length of a string. It counts characters until it hits the null terminator (\0), but it does not include the null terminator in the count.
#include <stdio.h> #include <string.h>int main() { char message[] = "Hello, World!"; // The %lu format specifier is used because strlen returns an unsigned long (size_t) printf("The length of the string is: %lu\n", strlen(message)); return 0; }
strcpy() and strncpy()In C, you cannot assign one string array to another using the = operator after they are initialized. You must copy the contents character by character.
strcpy(destination, source): Copies the source string to the destination.strncpy(destination, source, n): Copies up to n characters. This is much safer because it prevents buffer overflows if the source string is larger than the destination array.#include <stdio.h> #include <string.h>int main() { char source[] = "IntricateDevo"; char dest1[20]; char dest2[10]; // Using strcpy (Unsafe if source > destination) strcpy(dest1, source); printf("strcpy result: %s\n", dest1); // Using strncpy (Safe - limits copy to 9 chars + room for null terminator) // Notice we leave space for the null terminator manually! strncpy(dest2, source, sizeof(dest2) - 1); dest2[sizeof(dest2) - 1] = '\0'; // Always ensure null termination printf("strncpy result: %s\n", dest2); // Outputs: Intricate return 0; }
strcat() and strncat()Concatenation means appending one string to the end of another.
strcat(destination, source): Appends the source string to the destination string.strncat(destination, source, n): Safely appends up to n characters.⚠️ Crucial Safety Rule: The destination array must be large enough to hold both its original content AND the new content being added. If it is too small, your program will crash.
#include <stdio.h> #include <string.h>int main() { // Array size is 50 to ensure plenty of space char greeting[50] = "Hello, "; char name[] = "Developer!"; // Join name onto greeting strcat(greeting, name); printf("%s\n", greeting); // Output: Hello, Developer! return 0; }
strcmp()To check if two strings are identical, you cannot use if (str1 == str2). That only checks if they occupy the same memory address! You must use strcmp().
strcmp(str1, str2) compares two strings character by character based on their ASCII values.
Return values:
0: The strings are exactly identical.< 0: The first string is alphabetically smaller (comes before) the second.> 0: The first string is alphabetically greater (comes after) the second.#include <stdio.h> #include <string.h>int main() { char password[] = "secret123"; char input[] = "secret123"; // Check if strcmp returns 0 (meaning match) if (strcmp(password, input) == 0) { printf("Access Granted!\n"); } else { printf("Access Denied.\n"); } return 0; }
strchr() and strstr()Sometimes you need to search within a string to find a specific letter or word.
strchr(string, char): Returns a pointer to the first occurrence of a specific character.strstr(string, substring): Returns a pointer to the first occurrence of a whole word/substring.If the character or substring is not found, these functions return NULL.
#include <stdio.h> #include <string.h>int main() { char text[] = "Learning C programming is fun."; char word[] = "programming"; // Find a word char *result = strstr(text, word); if (result != NULL) { // The pointer points to the start of "programming..." printf("Found substring starting at: '%s'\n", result); } else { printf("Substring not found.\n"); } return 0; }
n versions: When dealing with user input or dynamic data, always use strncpy, strncat, and strncmp. They allow you to define a limit, preventing catastrophic buffer overflows.strncpy, it does not automatically add a null terminator if the limit n is reached. Always manually terminate your strings (e.g., str[size - 1] = '\0';).If strcmp(str1, str2) returns 0, what does it mean?
Which function is safer to use to prevent buffer overflows when copying strings?