String functions from <string.h>
Use the standard C string library — strcpy, strcat, strcmp, strncpy, strstr, and others — to copy, compare, and search strings safely.
- Use strcpy and strncpy to copy strings
- Use strcat and strncat to concatenate strings
- Use strcmp to compare strings alphabetically
- Use strstr and strchr to search within strings
The C standard library provides a set of string functions in <string.h>. These functions are the building blocks for working with text in C. They are also a source of notorious bugs — particularly buffer overflows — because they require you to manage destination buffer sizes yourself.
strcpy — copy a string
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "Hello";
char dst[10]; /* must be large enough */
strcpy(dst, src);
printf("%s\n", dst); /* Hello */
return 0;
}strcpy(dst, src) copies bytes from src to dst including the null terminator. dst must have enough space — strcpy does not check. If dst is too small, you write past its end: a buffer overflow.
Use strncpy for safer copying with a length limit:
char dst[10];
strncpy(dst, src, sizeof(dst) - 1); /* copy at most 9 chars */
dst[sizeof(dst) - 1] = '\0'; /* always null-terminate manually */strncpy does not guarantee null-termination if the source is longer than the limit, so you must add '\0' manually.
strlen — string length
size_t len = strlen("Hello"); /* 5 -- does not count '\0' */Remember: strlen runs in O(n) time because it scans to the null terminator. Do not call it inside a loop to check the length repeatedly — store it in a variable.
strcat — concatenate strings
char result[20] = "Hello";
strcat(result, ", World!"); /* appends -- result must have enough space */
printf("%s\n", result); /* Hello, World! */strcat appends src to the end of dst, overwriting the null terminator of dst and adding a new one at the end. Again, dst must be large enough for the combined result.
Safer version:
strncat(result, extra, sizeof(result) - strlen(result) - 1);strcmp — compare strings
You cannot use == to compare strings in C — that compares pointers (addresses), not content. Use strcmp:
int cmp = strcmp("apple", "banana");
/* cmp < 0: "apple" comes before "banana" alphabetically */
/* cmp == 0: the strings are equal */
/* cmp > 0: "apple" comes after the other string */#include <string.h>
int main(void) {
char input[] = "quit";
if (strcmp(input, "quit") == 0) {
printf("Exiting\n");
} else if (strcmp(input, "help") == 0) {
printf("Showing help\n");
}
return 0;
}strncmp(a, b, n) compares at most n characters. Use it when comparing a fixed-length prefix.
strchr and strrchr — searching for a character
char str[] = "hello world";
char *pos = strchr(str, 'o'); /* pointer to first 'o' */
char *last = strrchr(str, 'o'); /* pointer to last 'o' */
if (pos != NULL) {
printf("Found 'o' at index %td\n", pos - str); /* 4 */
}strchr returns a pointer to the first occurrence of the character, or NULL if not found. strrchr finds the last occurrence. The index is computed as pointer arithmetic (pos - str).
strstr — searching for a substring
char haystack[] = "the quick brown fox";
char *found = strstr(haystack, "quick");
if (found != NULL) {
printf("Found at index %td\n", found - haystack); /* 4 */
}sprintf — building a string with printf formatting
#include <stdio.h>
int main(void) {
char buffer[64];
int score = 95;
sprintf(buffer, "Your score is %d%%", score);
printf("%s\n", buffer); /* Your score is 95% */
return 0;
}sprintf works like printf but writes to a buffer instead of stdout. Always ensure the buffer is large enough. snprintf(buffer, size, format, ...) is safer — it limits output to size - 1 characters and always null-terminates.
Prefer snprintf over sprintf. sprintf has no length limit — it can overflow the buffer if the formatted output is longer than expected. snprintf takes a size argument and stops writing at that limit. This is one of the most important safe-coding habits in C.
Summary of key functions
| Function | Purpose | Safe alternative |
|---|---|---|
strlen(s) | Length of string | — |
strcpy(dst, src) | Copy string | strncpy |
strcat(dst, src) | Append string | strncat |
strcmp(a, b) | Compare strings | strncmp |
strchr(s, c) | Find first char | — |
strstr(s, sub) | Find substring | — |
sprintf(buf, fmt, ...) | Format into buffer | snprintf |
Where to go next
Next: common string pitfalls — the bugs that arise most often when working with C strings, including off-by-one errors, missing null terminators, and using == to compare strings.
C strings: char arrays and the null terminator
Understand how C represents text as null-terminated char arrays — declaration, the null terminator, string literals, and basic iteration.
Common string pitfalls
Recognise and avoid the most frequent C string bugs — missing null terminators, buffer overflows, comparing with ==, and off-by-one errors.