Home > Tutorials > C Tutorials > Programming In C: More Library Functions – strcmp, stricmp, strcat, strstr

Programming In C: More Library Functions – strcmp, stricmp, strcat, strstr

strcmp: compare two strings

You can compare the characters and spaces in a string using the strcmp function. For example, is “William Lambton” the same as “William Lamberton”?

Using strcmp, the computer will compare each character and space, to see if they are identical. If the names are the same, the computer will return a value of 0. We can express this in a conditional:

if(strcmp(one, two) == 0)

printf(“The names are the same.”);

else

printf(“The names are different.”);

Let’s put this conditional inside a program:

#include<stdio.h>
#include<string.h>
void main( )
{
char one[20] = “William Lambton”;
char two[20] = “William Lamberton”;
if(strcmp(one, two) == 0)
printf(“The names are the same.”);
else

printf(“The names are different.”);

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

stricmp: ignore uppercase/lowercase differences

This string function works the same as strcmp, except that it ignores the differences between uppercase and lowercase letters.

Here is an example:

#include<stdio.h>
#include<string.h>
void main( )
{
char upper[8] = “Everest”;
char lower[8] = “everest”;
if(stricmp(upper, lower) == 0)
printf(“If you ignore upper and lower case, then %s and %s are the same.”, upper, lower);else

printf(“They are different.”);}

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

strcat: concatenating strings

To concatenate means to joing two strings together. We use the strcat function for this:

#include<stdio.h>
#include<string.h>
void main( )
{
char name[ ] = “William Lambton”;
char job[ ] = ” led the Great Trigonometrical Survey of India.”;
strcat(name, job);
printf(“%s”, name);
}
 

Notice that we left the variable numbers empty for name[ ] and job[ ]. This is very important. When you concatenate strings, you must have enough room in the first string to add the entire second string. Leaving the brackets empty [ ] insures that you will have the necessary space.

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

strstr: searching for patterns in strings

We can search for a particular letter, word, or phrase in a string, using the strstr function. If the computer does not find the pattern, it will return NULL. Here is a sample program:

if(strstr(years, person) == NULL)

This says, if the computer searches “years” and can’t find “person”, then the match is NULL (no match).

Here is the complete program:

#include<stdio.h>
#include<string.h>
void main( )
{
char years[60] = “From 1803-1805 Lambton mapped the width of southern India.”;
char person[8] = “Lambton”;
if(strstr(years, person) == NULL)
printf(“%s did not map southern India.”, person);else

printf(“%s mapped southern India.”, person);}

Type this source code in your editor and save it as width.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()

  • tt

    “Leaving the brackets empty [ ] insures that you will have the necessary space.” This is wrong, your program contains a potential segfault. The correct way is to malloc the necessary number of chars.

  • tt

    “Leaving the brackets empty [ ] insures that you will have the necessary space.” This is wrong, your program contains a potential segfault. The correct way is to malloc the necessary number of chars.

  • tt

    “Leaving the brackets empty [ ] insures that you will have the necessary space.” This is wrong, your program contains a potential segfault. The correct way is to malloc the necessary number of chars.

  • tt

    “Leaving the brackets empty [ ] insures that you will have the necessary space.” This is wrong, your program contains a potential segfault. The correct way is to malloc the necessary number of chars.

  • tt

    “Leaving the brackets empty [ ] insures that you will have the necessary space.” This is wrong, your program contains a potential segfault. The correct way is to malloc the necessary number of chars.

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