The C string library

For an overview of the string functions in C, refer to the string manual page.

Find the length of a string

Use strlen to find the length of a string.

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

int main ()
{
    char * string = "This is a C string";
    printf ("The length of '%s' is %d.\n", string, strlen (string));
    return 0;
}

(download)

This prints

The length of 'This is a C string' is 18.

Find a substring in a string

Find a substring in a string using strstr. For example,

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

int main ()
{
    const char * haystack = "the quick brown fox jumped over the lazy dog";
    const char * needle = "the";
    char * s;
    const char * start;

    start = haystack;
    
    while (s = strstr (start, needle)) {
        printf ("Found %d characters in.\n", s - haystack);
        start = s + strlen (needle);
    }
    return 0;
}

(download)

This prints

Found 0 characters in.
Found 32 characters in.

Find a substring regardless of case

Find a substring in a string regardless of case using strcasestr. See Compare strings ignoring case in C for an example.

Find a substring with zero bytes

If your strings might contain zero bytes, use memmem to find substrings.

Find a character in a string

Use strchr or strrchr.

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

int main ()
{
    const char * haystack = "Giant Haystack";
    int pos = 'a';
    const char * location;

    location = haystack;
    while (1) {
	location = strchr (location, pos);
	/* strchr returns NULL (0) if the character is not found. */
	if (! location) {
	    puts ("End of string.");
	    break;
	}
	printf ("Found %c at position %d\n", pos, location - haystack);
	/* Skip over this character and start looking again. */
	location++;
    }
    /* Now go right to left. */
    location = haystack;
    location = strrchr (location, pos);
    /* strrchr returns NULL (0) if the character is not found. */
    if (! location) {
	printf ("%c was not found.", pos);
    }
    else {
	printf ("Found rightmost %c at position %d\n",
		pos, location - haystack);
    }
    return 0;
}

(download)

This prints

Found a at position 2
Found a at position 7
Found a at position 11
End of string.
Found rightmost a at position 11

Find some characters in a string

To find any of a set of characters in a string, use strpbrk.

Find characters not in a charset

Use strcpsn.

Copy a string into new memory

Use strdup to copy the whole string into new memory, or strndup to copy a certain length of it.

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

int main ()
{
    const char * read_only = "monkey";
    char * duplicate;
    char * three_dup;
    duplicate = strdup (read_only);
    /* We can write to this string. */
    duplicate[0] = toupper (duplicate[0]);
    printf ("%s -> %s\n", read_only, duplicate);
    /* Now copy three characters. */
    three_dup = strndup (read_only, 3);
    /* It actually allocates four bytes, since a trailing 0 is added. */
    printf ("%s -> %s\n", read_only, three_dup);
    /* We need to free it when the program finishes. */
    free (duplicate);
    free (three_dup);
    return 0;
}

(download)

This prints

monkey -> Monkey
monkey -> mon

Web links


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer