Chapter 13
Chapter 13
Chapter 13
Strings
String Literals
• A string literal is a sequence of characters enclosed within double
quotes: "When you come to a fork in the road, take it."
• String literals may contain escape sequences. For example, each
\n character in the string
"Candy\nIs dandy\nBut liquor\nIs quicker.\n --Ogden Nash\n"
causes the cursor to advance to the next line: Candy
Is dandy
But liquor
Is quicker.
--Ogden Nash
\0
a b c \0
String Variables
• Any one-dimensional array of characters can be
used to store a string.
• A string must be terminated by a null character.
• Difficulties with this approach:
– It can be hard to tell whether an array of characters is
being used as a string.
– String-handling functions must be careful to deal
properly with the null character.
– Finding the length of a string requires searching for the
null character.
String Variables
• If a string variable needs to hold 80 characters, it
must be declared to have length 81:
#define STR_LEN 80 Defining a macro and then
… adding 1 separately is a common
practice.
char str[STR_LEN+1];
• Adding 1 to the desired length allows room for
the null character at the end of the string.
• The actual length of a string depends on the
position of the terminating null character.
• An array of STR_LEN + 1 characters can hold
strings with lengths between 0 and STR_LEN.
10 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings
date2 J u n e 1 4 \0 \0
printf("%-25.6s\n", str);
Are we
str1 a b c d \0 \0 \0 \0 \0 \0
strcat(str1, "def");
str1 a b c d e f \0 \0 \0 \0
strcpy(str2, "def");
str2 d e f \0 \0 \0 \0 \0 \0 \0
strcat(str1, str2);
str1 a b c d e f \0 \0 \0 \0
strcpy(str2, "def");
str2 d e f \0 \0 \0 \0 \0 \0 \0
strcat(str1, strcat(str2, "ghi"));
str2 d e f g h i \0 \0 \0 \0
str1 a b c d e f g h i \0
str1 a b c \0 \0 \0
Arrays of Strings
• There is more than one way to store an array of
strings.
• One option is to use a two-dimensional array of
characters, with one string per row:
char planets[][8] = {"Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus", "Neptune", "Pluto"};
Arrays of Strings
• Unfortunately, the planets array contains a fair
bit of wasted space (extra null characters):
Arrays of Strings
• Most collections of strings will have a mixture of
long strings and short strings.
• What we need is a ragged array, whose rows can
have different lengths.
• We can simulate a ragged array in C by creating
an array whose elements are pointers to strings:
char *planets[] = {"Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus", "Neptune", "Pluto"};
Arrays of Strings
• This small change has a dramatic effect on how
planets is stored:
Arrays of Strings
• To access one of the planet names, all we need do
is subscript the planets array.
• Accessing a character in a planet name is done in
the same way as accessing an element of a two-
dimensional array.
• A loop that searches the planets array for
strings beginning with the letter M:
for (i = 0; i < 9; i++)
if (planets[i][0] == 'M')
printf("%s begins with M\n", planets[i]);
Command-Line Arguments
• When we run a program, we’ll often need to
supply it with information.
• This may include a file name or a switch that
modifies the program’s behavior.
• Examples of the UNIX ls command:
ls
ls –l
ls -l remind.c
Command-Line Arguments
• Command-line information is available to all
programs, not just operating system commands.
• To obtain access to command-line arguments,
main must have two parameters:
int main(int argc, char *argv[])
{
…
}
• Command-line arguments are called program
parameters in the C standard.
Command-Line Arguments
• argc (“argument count”) is the number of
command-line arguments.
• argv (“argument vector”) is an array of pointers
to the command-line arguments (stored as strings).
• argv[0] points to the name of the program,
while argv[1] through argv[argc-1] point
to the remaining command-line arguments.
• argv[argc] is always a null pointer—a special
pointer that points to nothing.
– The macro NULL represents a null pointer.
Command-Line Arguments
• If the user enters the command line
ls -l remind.c
then argc will be 3, and argv will have the
following appearance:
Command-Line Arguments
• Since argv is an array of pointers, accessing
command-line arguments is easy.
• Typically, a program that expects command-line
arguments will set up a loop that examines each
argument in turn.
• One way to write such a loop is to use an integer
variable as an index into the argv array:
int i;
for (i = 1; i < argc; i++)
printf("%s\n", argv[i]);
59 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings
Command-Line Arguments
• Another technique is to set up a pointer to
argv[1], then increment the pointer repeatedly:
char **p;
for (p = &argv[1]; *p != NULL; p++)
printf("%s\n", *p);