Skip to main content
Engineering LibreTexts

6.2: String Manipulation

  • Page ID
  • A confusing aspect of C strings for beginners (especially those coming from BASIC or even Python) is how to manipulate them. That is, how do you copy one string to another, compare strings, extract a substring, and so forth? As strings are really arrays, you can’t assign one to the other as in a[] = b[]; Instead, we rely on a series of string functions found in the string library. To use these functions, you need to link your code with the string library and use #include <string.h> at the start of your code. To copy one string to another, use strcpy(). The template is:

    strcpy( destination, source );

    So, if you wanted to copy the contents of my_pet[] into n[], you could write:

    strcpy( &n[0], &my_pet[0] );

    If you’re awake at this point, you might ask “What’s with the ampersand?” Good question! What the string copy function needs are the starting addresses of the two arrays. In essence, all it does is copy a character at a time from the source to the destination. When it hits the trailing null it’s done. We’ve already seen the “address of” (&) operator earlier when we looked at scanf(). So, all we’re saying here is “For the source, start at the address of the first character of my_pet[], and for the destination, start at the first character of n[].” This can be a little cumbersome, so C offers a shortcut. You can think of & and [] as sort of canceling each other out. We’d normally write:

    strcpy( n, my_pet );

    Note that it is perfectly acceptable to use an index other than zero if you need to copy over just a chunk of the string. You could start copying from index 2 if you’d like, and just get “do” instead of “fido”:

    strcpy( n, &my_pet[2] );

    This can also be shortcut by using:

    strcpy( n, my_pet+2 );

    that is, don’t start at the address of the first element of my_pet[], start 2 characters later. We’ll look at this sort of manipulation much closer when we examine addresses and pointers.

    What happens if the source string has more characters than the destination string was allocated to? For example, what if you did this?

    strcpy( my_pet, n );

    This results in a memory overwrite that can accidentally destroy other variables or functions. Very bad! Your program may crash, and in some cases, your operating system my crash. To protect against this, you can use strncpy(). This places a limit on the number of characters copied by adding a third argument. As the destination only has space for 5 characters, you’d use:

    strncpy( my_pet, n, 5 );

    This function will stop at 5 characters. Unfortunately, it won’t automatically null terminate the string if the limit is reached. To be safe, you’d need to add the following line:

    my_pet[4] = 0; /* force null termination */

    Remember, as C counts from 0, index 4 is the fifth (and final) element. There are many functions available to manipulate strings as well as individual characters. Here is a short list:

    strcmp() Compares two strings (alphabetically)
    strcmpi() As above, but case insensitive
    strncmp() Compares two strings with max length
    strncat() Concatenate two strings with max length
    strlen() Find length of string (count of chars before null)

    The following work on single characters. Again this is just a sampling to give you an idea of what’s out there. Use #include <ctype.h>

    isupper() Determines if character is upper case
    isalpha() Determines if character is alphabetic (not numeral, punctuation, etc.)
    tolower() Turns character into lower case version

    If you don’t have library documentation, it can be very instructive to simply open various header files and look at the function prototypes to see what’s available. Whatever you do though, don’t edit these files!

    Finally, if you need to convert numeric strings into integer or floating point values, use the functions atoi(), atol() and atof(). (ASCII to int or long int in stdlib.h, ASCII to float in math.h).