UNIX C Programming

Chapter 11: Character Strings and String Functions


This chapter expands on the coverage given to creating and using strings and introduces string functions, character-testing functions, and string to number conversion functions. The objectives important to this chapter include:
  • Review the difference between a string and a character constant
  • Discuss the alternative ways of initializing strings
  • Compare string versus array notation differences
  • Introduce the gets() and puts() functions
  • Compare gets() and puts() to scanf() and printf()
  • Examine the five common string functions
  • Demonstrate the sorting of strings
  • Review the common character test functions
  • Use command line arguments with a C program
  • Explain the functions important to string to number conversion

A character string is a char array terminated by a \0. A string constant is anything enclosed in double quotation marks.

Character string constants are placed in the static storage class.

Character string arrays can be initialized in two ways. The shorthand way is:

char m1[] = "This is the fastest way.";

The longer way, using character constants is (abbreviated):

char m1[] = { 'T', 'h', 'i', .............'\0'};

Review the following relationships between array and pointer notation (assuming the m1 array above) :

m1 == &m1[0]  
/* name of array points to first element */

*m1 == 'T'  
/* contents of the array name pointer same as the
first element contents */

*(m1 + 1) == m1[1] =='h'  
/* adding one to pointer means to point
to next array element */

The chief difference between pointer and array notation is that with array notation the array name is a constant, while with pointer notation, the pointer to the string is a variable.

The notation for an array of strings is

char *name[size]

This notation leads to a ragged array, with each string taking only the bytes in memory as required.

Most C operations for strings work with pointers. Instead of copying a string, it is more efficient to copy the address which points to the string.

The get string function, gets(), gets an entire string from the keyboard until it reaches a newline character. It differs from scanf() which gets one word at a time from the keyboard.

The put string function, puts(), displays an entire string, ending when it reads an end-of-string marker (\0). Each puts() begins a new line of output.

The prototypes for gets() and puts() are found in stdio.h.

The C library contains more than 20 string functions, however, the most common are strlen(), strcat(), strcmp(), strcpy(), and sprintf(). The purpose of each function is as follows:

  • strlen() Returns the length of a string
  • strcat() Concatenates two strings, adding the second to the first
  • strcmp() Compares two strings to determine if they are identical
  • strcpy() Copies a string
  • sprintf() Writes to a string instead of to the display screen

String function prototypes are found in the string.h header file.

Character-testing functions include:

  • isalnum()
  • isalpha()
  • iscntrl()
  • isdigit()
  • isgraph()
  • islower()
  • isupper()
  • isprint()
  • ispunct()
  • isspace()
  • isupper()
  • isxdigit()

Two character mapping functions are toupper() and tolower().

These character function prototypes are found in the ctype.h header file.

The command line is the line you type to run your program. When command line arguments are used, main() is defined as:

int main(int argc, * argv[])

where argc stands for argument counter and argv for argument values.

Several functions allow for string to number conversion. These include atoi(), and atof(). Conversion from a number to a string is non-standard and, instead, use of sprintf()is encouraged.