C: String Functions

🗓 Last updated: 2026-11-30👤 Written by: Ivy✓ Reviewed by: MOTO🏷 Category: C Tutorial

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.

Reference entries in this section: strcpy strncpy strcat strcmp strncmp strlen sizeof strchr strstr

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:

C
// Preprocessor directive
#include <string.h>

2. strcpy and strncpy

(1) strcpy

strcpy copies the source string to the destination array, including \0:

C
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);

⚠️ Note: 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:

C
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.

💡 Tip: Always reserve space for \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:

C
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.

⚠️ Note: The destination array must have enough space for 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:

TEXT
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:

C
// Variable declaration
int result = strcmp("abc", "abd");

Return value meanings:

TEXT
if (strcmp(s1, s2) == 0) {
    printf("Equal\n");
}
⚠️ Note: Use strcmp to compare strings, never ==. == compares pointer addresses, not string contents.

(2) strncmp

strncmp compares only the first n characters:

C
// 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 Yourself
TEXT
alice
bob
charlie
david
eve

5. strlen

strlen returns the effective length of a string (not including \0):

C
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:

TEXT
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:

C
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:

TEXT
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:

C
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:

TEXT
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

C
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

C
// 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

C
// 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 Yourself
TEXT
Hello, 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

输出:

TEXT
strcmp(Apple, Banana) = -1 (negative means str1 < str2)
Found 'C' at position: 22
Found 'fun'!

Reference entries in this section: strcpy strncpy strcat strcmp strncmp strlen sizeof strchr strstr

❓ FAQ

Q Why doesn't strncpy automatically append \0?
A 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.
Q Does the specific numeric value returned by strcmp matter?
A You should only check whether it is positive, negative, or zero -- do not depend on the exact value. Different implementations return different differences; portable code only uses ==0, <0, >0.
Q strcat is inefficient for multiple concatenations -- what to do?
A Each 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.
Q What is the point of implementing string functions yourself?
A Understanding underlying principles, common interview questions, and embedded development where the standard library may not be available. However, prefer standard library functions in real projects -- they are thoroughly tested and optimized.

📖 Summary

📝 Exercises

  1. Write a function void str_tolower(char *s) that converts all uppercase letters in a string to lowercase.
  2. Write a function int str_count_char(const char *s, char ch) that counts how many times the character ch appears in string s.
  3. Without using any string.h functions, write my_strstr to implement substring search.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏