Programming In C: Library Functions for Strings – strcpy, strlen

C Library Functions for Strings

C comes with libraries of special functions, that we can use in our programs. These functions are stored in the string header file, <string.h>, which we include at the beginning of our program:

#include<string.h>

Here is a list of string library functions:

strcpy copies a string
strlen calculates the length of a string
strcmp compares two strings
stricmp compares two strings, ignoring uppercase/lowercase differences
strcat concatenates (joins) two strings
strstr searches a string for a pattern

Let’s examine each of these string library functions.

strcpy: copy a string

You can copy (replace) a string using the strcpy function. For example, I have a bagel every morning for breakfast. Sometimes I replace the bagel with a bowl of cereal. Here is how I would express that in C:

  1. include the string header: #include<string.h>
  2. initialize my variables:
    • char old[15] = “bagel”;
    • char new[15] = “bowl of cereal”;
  3. use the strcpy function to replace bagel with bowl of cereal: strcpy(old, new);
  4. print a statement about the bowl of cereal: printf(“\nToday I ate a %s”, old);

Let’s put it all together:

#include<stdio.h>
#include<string.h>
void main( )
{
char old[15] = “bagel”;
char new[15] = “bowl of cereal”;
printf(“\nYesterday I ate a %s”, old);
strcpy(old, new);
printf(“\nToday I ate a %s”, old);
}

Type this source code in your editor and save it as bagel.c then compile it, link it, and run it. Take note that because we are using the strcmp function to replace our old variable with our new variable, our old variable should be bigger or of equal length to our new one. Hence we specify char old[15]. (This correction was pointed out by Juan in the comments below).

Now let’s replace a variable twice. During the late 1700s and early 1800s, British surveyers mapped India. The three most famous surveyers were Colin Mackenzie, William Lambton, and George Everest (for whom Mt. Everest is named). Here is a program that presents this information, using the strcpy function:

#include<stdio.h>
#include<string.h>
void main( )
{
char one[20] = “Colin Mackenzie”;
char two[20] = “William Lambton”;
char three[20] = “George Everest”;
printf(“Three great surveyers mapped India.”);
printf(“\nFirst: %s”, one);
strcpy(onetwo);
printf(“\nSecond: %s”, one);
strcpy(onethree);
printf(“\nThird: %s”, one);
}

Type this source code in your editor and save it as india.c then compile it, link it, and run it.

strlen: string length

There is an easy way to calculate the length of a string. Use the strlen function:

  1. initialize a string variable with no number: char survey[ ]
  2. initialize a variable to store the length of the string: int length;
  3. calculate the length of the string using strlen: lengthstrlen(survey);

Here is the program:

#include<stdio.h>
#include<string.h>
void main( )
{
char survey[ ] = “The Great Trigonometical Survey of India”;
int length;
lengthstrlen(survey);
printf(“%s has %d characters.”, surveylength);
}

Type this source code in your editor and save it as trig.c then compile it, link it, and run it.

 

 

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

4 Comments

  1. hi, I fond a error in [ char old[6] = “bagel”;char new[15] = “bowl of cereal”;] if we use strcmp(old, new). the size of old should bigger or same than the size of new. So we should define as follows: [ char old[15] = “bagel”;char new[15] = “bowl of cereal”;] .Then you can run the code, and get the right result. Thanks very much for your C tutorials.

    1. Thank you for pointing this out @02c50fe87df907151997984fc227db28:disqus

      I have made the correction within the tutorial, and gave you credit for pointing it out.

Leave a comment

Your email address will not be published. Required fields are marked *