Learn How to Program in C

by
Dr. Kristin Switala

Introduction

Directives

Variables

Conditionals

Loops

Arrays

Pointers

Strings

Functions

Structures


Site Map

Strings (c)

In This Section:
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.

Copyright
© 2001
Kristin Switala
Hosted by www.Geocities.ws

1