C: String Functions
String functions are like wrenches and screwdrivers in a toolbox -- each tool solves a specific problem: copying, concatenating, comparing, searching. Master them, and you will handle text with confidence.
1. string.h Overview
<string.h> is the core header file for string operations in the C standard library, providing functions for copying, concatenating, comparing, and searching. You must include it before use:
// Preprocessor directive
#include <string.h>
2. strcpy and strncpy
(1) strcpy
strcpy copies the source string to the destination array, including \0:
char dest[20];
// String/memory operation
strcpy(dest, "Hello World");
// I/O operation
printf("%s\n", dest);
Prototype: char *strcpy(char *dest, const char *src);
strcpy does not check the destination buffer size. If the source string is larger than the destination array, it writes out of bounds. Ensure the destination array is large enough before using it.
(2) strncpy
strncpy limits the maximum number of bytes to copy, making it safer:
char dest[6];
strncpy(dest, "Hello World", sizeof(dest) - 1);
dest[sizeof(dest) - 1] = '\0';
// I/O operation
printf("%s\n", dest);
Prototype: char *strncpy(char *dest, const char *src, size_t n);
strncpy copies at most n bytes. If the source string is shorter than n, the remaining positions are filled with \0; if the source string is n bytes or longer, it does not automatically append \0. Therefore, you must add it manually.
\0 when using strncpy, and manually append \0 after copying.
3. strcat and strncat
(1) strcat
strcat appends the source string to the end of the destination string:
char buf[50] = "Hello";
// String/memory operation
strcat(buf, " World");
// I/O operation
printf("%s\n", buf);
Prototype: char *strcat(char *dest, const char *src);
The existing \0 at the end of the destination string is overwritten, and a new \0 is placed after the concatenated result.
strcat does not check the buffer size.
(2) strncat
strncat limits the maximum number of characters to append and automatically adds \0:
char buf[10] = "Hi";
strncat(buf, " World!", sizeof(buf) - strlen(buf) - 1);
printf("%s\n", buf);
Prototype: char *strncat(char *dest, const char *src, size_t n);
It appends at most n characters, then automatically adds \0. It is safer than strncpy because it guarantees null termination.
4. strcmp and strncmp
(1) strcmp
strcmp compares two strings lexicographically, character by character:
// Variable declaration
int result = strcmp("abc", "abd");
Return value meanings:
< 0: s1 is less than s2= 0: s1 equals s2> 0: s1 is greater than s2
if (strcmp(s1, s2) == 0) {
printf("Equal\n");
}
strcmp to compare strings, never ==. == compares pointer addresses, not string contents.
(2) strncmp
strncmp compares only the first n characters:
// Conditional check
if (strncmp(str, "GET ", 4) == 0) {
// I/O operation
printf("GET request\n");
}
Useful for comparing prefixes or limiting the comparison scope.
▶ Example
Sort an array of strings in lexicographic order:
▶ Try it Yourselfalice
bob
charlie
david
eve
5. strlen
strlen returns the effective length of a string (not including \0):
char s[] = "Hello";
// I/O operation
printf("%zu\n", strlen(s));
// I/O operation
printf("%zu\n", sizeof(s));
Output: 5 and 6. strlen counts up to \0; sizeof includes \0.
strlen traverses the string to find \0, with O(n) time complexity. If you use the same string's length multiple times, cache the result:
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) {
}
Rather than calling strlen in the loop condition every iteration.
6. strchr and strrchr
strchr finds the first occurrence of a character in a string:
const char *p = strchr("Hello World", 'o');
// Conditional check
if (p != NULL) {
// I/O operation
printf("Found: %s\n", p);
}
Output: "o World". It returns a pointer to that character, or NULL if not found.
strrchr finds the last occurrence of a character:
const char *p = strrchr("Hello World", 'o');
if (p != NULL) {
printf("Last occurrence: %s\n", p);
}
Output: "orld".
7. strstr
strstr finds the first occurrence of a substring in a string:
const char *p = strstr("Hello World", "World");
// Conditional check
if (p != NULL) {
// I/O operation
printf("Substring at: %s\n", p);
}
Output: "World". Returns NULL if not found.
Finding all occurrences:
const char *text = "abababab";
const char *p = text;
while ((p = strstr(p, "ab")) != NULL) {
printf("Position %ld\n", (long)(p - text));
p++;
}
After each find, advance the pointer by one and continue searching for subsequent occurrences.
8. Custom String Function Implementations
Understanding how standard functions work internally helps you grasp the essence of string operations.
(1) Custom strlen
size_t my_strlen(const char *s) {
size_t len = 0;
// While loop
while (s[len] != '\0') {
len++;
}
// Return result
return len;
}
(2) Custom strcpy
// Function definition
char *my_strcpy(char *dest, const char *src) {
// Variable declaration
char *d = dest;
// While loop
while ((*d++ = *src++) != '\0') {
}
// Return result
return dest;
}
This compact form: *d++ = *src++ copies the character first, then both pointers advance, until \0 is copied.
(3) Custom strcmp
// Function definition
int my_strcmp(const char *s1, const char *s2) {
// While loop
while (*s1 == *s2) {
// Conditional check
if (*s1 == '\0') return 0;
s1++;
s2++;
}
// Return result
return (unsigned char)*s1 - (unsigned char)*s2;
}
Compare character by character: continue while equal, stop when a difference or \0 is found. Return the difference to indicate the ordering. Casting to unsigned char ensures negative character values do not affect the comparison result.
▶ Example
Custom strcat and test:
▶ Try it YourselfHello, World!
First find the \0 at the end of dest, then copy the contents of src starting from that position.
▶ Example 3: Compare and Find in a String
▶ Try it Yourself输出:
TEXTstrcmp(Apple, Banana) = -1 (negative means str1 < str2) Found 'C' at position: 22 Found 'fun'!
❓ FAQ
\0?strncpy was originally designed to fill fixed-length buffers (like filename fields): pad with \0 when shorter than n, but do not add \0 when n bytes are reached. When used as a safer strcpy, you must add \0 manually.strcmp matter?==0, <0, >0.strcat is inefficient for multiple concatenations -- what to do?strcat scans from the beginning of dest to the end before appending, making the total efficiency O(n^2) for multiple concatenations. You can manually track the current end position and append directly from there.📖 Summary
strcpy/strncpycopy strings; strncpy is safer but requires manual\0terminationstrcat/strncatconcatenate strings; strncat automatically adds\0strcmp/strncmpcompare strings; never use==to compare string contentsstrchr/strrchrsearch for characters;strstrsearches for substrings- Custom implementations help understand underlying principles; use standard library in real projects
📝 Exercises
- Write a function
void str_tolower(char *s)that converts all uppercase letters in a string to lowercase. - Write a function
int str_count_char(const char *s, char ch)that counts how many times the characterchappears in strings. - Without using any string.h functions, write
my_strstrto implement substring search.