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

Understanding The <BASE> Tag in 2018

The <BASE> tag has been a part of HTML for decades now, and little has ...

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