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:
- include the string header: #include<string.h>
- initialize my variables:
- char old[15] = “bagel”;
- char new[15] = “bowl of cereal”;
- use the strcpy function to replace bagel with bowl of cereal: strcpy(old, new);
- 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(one, two); printf(“\nSecond: %s”, one); strcpy(one, three); 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:
- initialize a string variable with no number: char survey[ ]
- initialize a variable to store the length of the string: int length;
- calculate the length of the string using strlen: length = strlen(survey);
Here is the program:
#include<stdio.h> #include<string.h> void main( ) { char survey[ ] = “The Great Trigonometical Survey of India”; int length; length = strlen(survey); printf(“%s has %d characters.”, survey, length); } |
Type this source code in your editor and save it as trig.c then compile it, link it, and run it.
almost all the above functions looks same… thanks
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.
Thank you for pointing this out @02c50fe87df907151997984fc227db28:disqus
I have made the correction within the tutorial, and gave you credit for pointing it out.