C <string.h>

C <string.h> Library: String Manipulation

In 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>

1. Getting String Length: 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.

strlen() Example

#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; }


2. Copying Strings: 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.

String Copy Example

#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; }


3. Concatenating (Joining) Strings: strcat() and strncat()

Concatenation means appending one string to the end of another.

⚠️ 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.

String Concatenation Example

#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; }


4. Comparing Strings: 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:

String Compare Example

#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; }


5. Finding Substrings and Characters: strchr() and strstr()

Sometimes you need to search within a string to find a specific letter or word.

If the character or substring is not found, these functions return NULL.

Search Example

#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; }


Summary of Best Practices


Exercise 1 of 2

?

If strcmp(str1, str2) returns 0, what does it mean?

Exercise 2 of 2

?

Which function is safer to use to prevent buffer overflows when copying strings?