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

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.

 

 

About 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.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

  • Pingback: Rapid Purple » Programming In C: Built In & User Defined Functions()

  • almost all the above functions looks same… thanks

  • juan

    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.

    • Metalp3n

      Thank you for pointing this out @02c50fe87df907151997984fc227db28:disqus

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

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close