C

String Handling Functions in C

In C, a string is not a formal data type but a one-dimensional array of characters terminated by a null character (\0). While you can manipulate strings manually using loops, the C Standard Library provides a powerful header file, <string.h>, which contains pre-defined functions to perform common operations like copying, concatenating, comparing, and searching.

1. The Theory: Memory and the Null Terminator

To understand string functions, you must understand how strings are stored. If you define char name[] = "C-Prog";, the memory looks like this:

Index0123456
Value'C''-''P''r''o''g''\0'

The null terminator (\0) is essential. It tells string functions where the data ends. Without it, functions like printf or strlen would continue reading into adjacent memory until they crash or find a random zero byte.

2. Common String Handling Functions

The <string.h> library must be included at the top of your program to use these functions.

A. strlen() – String Length

Calculates the number of characters in a string excluding the null terminator.

  • Theory: It starts at the first character and increments a counter until it hits \0.

  • Syntax: size_t length = strlen(str);

B. strcpy() – String Copy

Copies the content of the "source" string into the "destination" string.

  • Theory: It copies character by character, including the \0. The destination array must be large enough to hold the source; otherwise, it causes a buffer overflow.

  • Syntax: strcpy(destination, source);

C. strcat() – String Concatenation

Appends (joins) the "source" string to the end of the "destination" string.

  • Theory: It finds the \0 of the destination, removes it, appends the source characters, and then adds a new \0 at the very end.

  • Syntax: strcat(destination, source);

D. strcmp() – String Comparison

Compares two strings lexicographically (based on ASCII values).

  • Theory: It subtracts the ASCII value of the characters at the same position.

    • Returns 0 if strings are identical.

    • Returns a positive value if the first differing character in str1 is greater than str2.

    • Returns a negative value if it is smaller.

  • Syntax: int result = strcmp(str1, str2);

E. strrev() – String Reverse

Reverses the characters in a string.

  • Note: This is a non-standard function found in some compilers (like Turbo C or MinGW). In standard C (GCC), you typically write your own logic for this.

3. Practical Code Example

This program demonstrates the most frequently used string functions in a single workflow.


#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[20] = "World";
    char str3[40];

    // 1. strlen
    printf("Length of str1: %zu\n", strlen(str1));

    // 2. strcpy: Copy str1 into str3
    strcpy(str3, str1);
    printf("After strcpy, str3: %s\n", str3);

    // 3. strcat: Join str2 to str3
    strcat(str3, " ");
    strcat(str3, str2);
    printf("After strcat, str3: %s\n", str3);

    // 4. strcmp: Compare str1 and str2
    if (strcmp(str1, str2) == 0) {
        printf("Strings are equal.\n");
    } else {
        printf("Strings are different.\n");
    }

    return 0;
}

4. Safety and Modern Alternatives

One of the biggest security risks in C is Buffer Overflow. If you use strcpy to move a 100-character string into a 10-character array, C will overwrite other data, leading to crashes or hacks.

To prevent this, many modern developers use "n-versions" of these functions:

  • strncpy(dest, src, n): Copies at most n characters.

  • strncat(dest, src, n): Appends at most n characters.

  • strncmp(s1, s2, n): Compares only the first n characters.

5. Summary Table

FunctionPurposeHeaderReturns
strlen()Get length<string.h>Number of chars (size_t)
strcpy()Copy string<string.h>Pointer to destination
strcat()Join strings<string.h>Pointer to destination
strcmp()Compare strings<string.h>0, >0, or <0
strlwr()Convert to lower<string.h>Pointer to string
strupr()Convert to upper<string.h>Pointer to string
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now