Basics of String Functions for C Programming - dummies

Basics of String Functions for C Programming

By Dan Gookin

Despite its nonvariable type classification, the C programming library doesn’t skimp on functions that manipulate strings. Just about anything you desire to do with a string can be done by using some of the many string functions. And when those functions fall short, you can write your own.

Introduction to string functions

Here are some of the C language library functions that manipulate or abuse strings:

Function What It Does
strcmp() Compares two strings in a case-sensitive way. If the strings
match, the function returns 0.
strncmp() Compares the first n characters of two strings, returning 0 if
the given number of characters match.
strcasecmp() Compares two strings, ignoring case differences. If the strings
match, the function returns 0.
strncasecmp() Compares a specific number of characters between two strings,
ignoring case differences. If the number of characters match, the
function returns 0.
strcat() Appends one string to another, creating a single string out of
two.
strncat() Appends a given number of characters from one string to the end
of another.
strchr() Searches for a character within a string. The function returns
that character’s position from the start of the string as a
pointer.
strrchr() Searches for a character within a string, but in reverse. The
function returns the character’s position from the end of the
string as a pointer.
strstr() Searches for one string inside another string. The function
returns a pointer to the string’s location if it’s
found.
strnstr() Searches for one string within the first n characters of the
second string. The function returns a pointer to the string’s
location if it’s found.
strcpy() Copies (duplicates) one string to another.
strncpy() Copies a specific number of characters from one string to
another.
strlen() Returns the length of a string, not counting the or NULL
character at the end of the string.

More string functions are available. Many of them do specific things that require a deeper understanding of C. The ones shown are the most common.

All of these string functions require the string.h header file to be included with your source code:

#include <string.h>

On a Unix system, you can review all the string functions by typing the command man string in a terminal window.

Text of string functions

Strings are compared by using the strcmp() function and all its cousins: strncmp(), strcasecmp(), and strncasecmp().

The string-comparison functions return an int value based on the result of the comparison: 0 for when the strings are equal, or a higher or lower int value based on whether the first string’s value is greater than (higher in the alphabet) or less than (lower in the alphabet) the second string. Most of the time, you just check for 0.

Let Me In uses the strcmp() function in Line 13 to compare the initialized string password with whatever text is read at Line 11, which is stored in the input array. The result of that operation is stored in the match variable, which is used in an if-else decision tree at Line 14 to display the results.

LET ME IN

#include <stdio.h>
#include <string.h>
int main()
{
 char password[]="taco";
 char input[15];
 int match;
 printf("Password: ");
 scanf("%s",input);
 match=strcmp(input,password);
 if(match==0)
 puts("Password accepted");
 else
 puts("Invalid password. Alert the authorities.");
 return(0);
}

Exercise 1: Type the source code from Let Me In into your editor. Try out the program a few times to ensure that it accepts only taco as the proper password.

Exercise 2: Eliminate the match variable from your code in Exercise 13-7 and use the strcmp() function directly in the if comparison. That’s the way most programmers do it.

Exercise 3: Ratchet down security a notch by replacing the strcmp() function with strcasecmp(). Run the program to confirm that both taco and TACO are accepted as the password.

How to build strings

The glue that sticks one string onto the end of another is the strcat() function. The term cat is short for concatenate, which means to link together. Here’s how it works:

strcat(first,second);

After this statement executes, the text from the second string is appended to the first string. Or you can use immediate values:

strcat(gerund,"ing");

This statement tacks the text ing onto the end of the gerund text array.

The code in Introductions declares two char arrays to hold text. Array first is twice as large as array last because it’s the location where the second string’s content is copied. The copying takes place at Line 13 with the strcat() function.

INTRODUCTIONS

#include <stdio.h>
#include <string.h>
int main()
{
 char first[40];
 char last[20];
 printf("What is your first name? ");
 scanf("%s",first);
 printf("What is your last name? ");
 scanf("%s",last);
 strcat(first,last);
 printf("Pleased to meet you, %s!n",first);
 return(0);
}

Exercise 4: Create a new program by using the source code from Introductions. Run the program. Type in your first and last names, and then do Exercise 5.

Exercise 5: Modify your source code so that a single space is concatenated to the first string before the last string is concatenated.