String Methods

What Are String Functions in C? With Examples

Learn the string functions in C from <string.h>, such as strlen, strcpy, strcat, strcmp, and strstr, with short examples and buffer-safety tips.

On this page
  1. The main string functions
  2. Finding the length with strlen
  3. Copying with strcpy and strncpy
  4. Joining with strcat and strncat
  5. Comparing with strcmp and strncmp
  6. Searching with strchr and strstr
  7. Keeping copies safe
  8. Moving to std::string

String functions in C are library functions that work on null-terminated character arrays. They are declared in <string.h>, and C++ programs reach the same functions through <cstring>. The examples below use <string.h> and <stdio.h> so that they compile as both C and C++.

The main string functions

Function Purpose
strlen(s) Returns the length, not counting the '\0'
strcpy(dest, src) Copies src into dest, including the '\0'
strncpy(dest, src, n) Copies at most n characters from src
strcat(dest, src) Appends src to the end of dest
strncat(dest, src, n) Appends at most n characters from src
strcmp(a, b) Returns 0 when the strings are equal
strncmp(a, b, n) Compares at most the first n characters
strchr(s, c) Returns a pointer to the first c, or NULL
strstr(s, sub) Returns a pointer to the first sub, or NULL

Not every function you see in examples is standard. For instance, strrev() is not part of <string.h>, and Why is strrev() not working in C? looks at what that means in practice.

Finding the length with strlen

strlen() counts characters until it reaches the null character, and it does not count the null character itself. It does not know the size of the array, so it relies on the terminator.

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

int main(void) {
    char word[] = "library";
    printf("%zu\n", strlen(word));  /* 7 */
    return 0;
}

Copying with strcpy and strncpy

strcpy() copies the source, including its null character, into the destination. It does not check the size of the destination.

char name[10];
strcpy(name, "Grace");  /* name now holds "Grace" */

strncpy() copies at most n characters. When the source is at least n characters long, it does not add a null character, so you may need to add one yourself.

char code[4];
strncpy(code, "ABCDEF", 3);
code[3] = '\0';  /* strncpy did not add one */
printf("%s\n", code);  /* ABC */

Joining with strcat and strncat

strcat() adds the source to the end of the destination. The destination must already hold a valid string, and it must have room for the new text and the null character.

char greeting[20] = "Hello";
strcat(greeting, ", ");
strcat(greeting, "world");  /* "Hello, world" */

strncat() adds at most n characters from the source and then adds a null character. For example, strncat(greeting, "!!!", 1) adds one exclamation mark.

Comparing with strcmp and strncmp

strcmp() returns 0 when two strings are equal. A negative result means the first string sorts before the second, and a positive result means it sorts after.

char a[] = "apple";
char b[] = "apricot";

if (strcmp(a, b) < 0) {
    printf("apple comes first\n");
}
if (strncmp(a, b, 2) == 0) {
    printf("the first two letters match\n");
}

strncmp() compares only the first n characters, which is useful for checking a prefix.

Searching with strchr and strstr

strchr() finds the first occurrence of one character, and strstr() finds the first occurrence of a substring. Both return a pointer to the match, or NULL when nothing matches. The pointer points inside the original string, not to a copy. The strstr() function gets its own explanation in What does strstr() do in C.

char text[] = "the cat sat";
char *hit = strstr(text, "cat");  /* points to "cat sat" */

if (hit != NULL) {
    printf("%s\n", hit);  /* cat sat */
}

Keeping copies safe

Most bugs with these functions come from a destination that is too small. A string of five visible characters needs an array of at least six elements, because the null character takes one slot.

snprintf() is a safer choice for copying. It never writes more than the size you give it, and it always adds a null character when the size is at least 1.

char title[8];
snprintf(title, sizeof title, "%s", "A very long title");  /* "A very " */

In C, compare the result of strcmp() against 0. Comparing two strings with == compares their addresses instead.

Moving to std::string

In C++, std::string manages its own size and memory, so you do not have to size a buffer yourself. Its member functions are listed in C++ String Methods. To move a result from a char array into a std::string, see how to convert C string to string.

These functions cover the everyday cases for C strings. Keep the terminator in mind, and most of the bugs above become easier to spot.

The complete guide to string methods

C++ String Methods
Read the guide

Keep exploring

Guides from other topics

All topics →