0% found this document useful (0 votes)
28 views60 pages

Chapter 13

Uploaded by

Wassim Dobbi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views60 pages

Chapter 13

Uploaded by

Wassim Dobbi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Chapter 13: Strings

Chapter 13

Strings

1 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
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

• The backslash character (\) can be used to continue a string


literal from one line to the next:
printf("When you come to a fork in the road, take it. \
--Yogi Berra");
2 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

How String Literals Are Stored


• Strings are arrays of characters in which a special
character—the null character—marks the end.
• When a C compiler encounters a string literal of
length n in a program, it sets aside n + 1 bytes of
memory for the string.
• This memory will contain the characters in the
string, plus one extra character—the null
character—to mark the end of the string.
• The null character is a byte whose bits are all zero,
so it’s represented by the \0 escape sequence.
3 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

How String Literals Are Stored


• A string literal is stored as an ak, the compiler
treats it as a pointer of type char *.
• The string literal "abc" is stored as an array of
four characters:
a b c \0

• The string "" is stored as a single null character:

\0

4 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Operations on String Literals


• We can use a string literal wherever C allows a
char * pointer:
char *p;
p = "abc";
• This assignment makes p point to the first
character of the string.

a b c \0

5 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Operations on String Literals


• String literals can be subscripted:
char ch;
ch b
ch = "abc"[1];
The new value of ch will be the letter b.
• A function that converts a number between 0 and
15 into the equivalent hex digit:
char digit_to_hex_char(int digit)
{
return "0123456789ABCDEF"[digit];
}

6 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Operations on String Literals


• Attempting to modify a string literal causes
undefined behavior:
char *p = "abc";
*p = 'd'; /*** WRONG ***/
• A program that tries to change a string literal may
crash or behave erratically.

7 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

String Literals versus Character Constants


• A string literal containing a single character isn’t
the same as a character constant.
– "a" is represented by a pointer.
– 'a' is represented by an integer.
• A legal call of printf:
printf("\n");
• An illegal call:
printf('\n'); /*** WRONG ***/

8 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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.

9 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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

Initializing a String Variable


• A string variable can be initialized at the same
time it’s declared:
char date1[8] = "June 14";
• The compiler will automatically add a null
character so that date1 can be used as a string:
date1 J u n e 1 4 \0

• "June 14" is not a string literal in this context.


• Instead, C views it as an abbreviation for an array
initializer.
11 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Initializing a String Variable


• If the initializer is too short to fill the string
variable, the compiler adds extra null characters:
char date2[9] = "June 14";
Appearance of date2:

date2 J u n e 1 4 \0 \0

12 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Initializing a String Variable


• An initializer for a string variable can’t be longer
than the variable, but it can be the same length:
char date3[7] = "June 14";
• There’s no room for the null character, so the
compiler makes no attempt to store one:
date3 J u n e 1 4

13 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Initializing a String Variable


• The declaration of a string variable may omit its
length, in which case the compiler computes it:
char date4[] = "June 14";
• The compiler sets aside eight characters for
date4, enough to store the characters in "June
14" plus a null character.
date4 J u n e 1 4 \0
• Omitting the length of a string variable is
especially useful if the initializer is long, since
computing the length by hand is error-prone.
14 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Character Arrays versus Character Pointers


In the array
version, the
In the array characters
version, date • The declaration stored in
is an array date can be
name. char date[] = "June 14";
modified.
declares date to be an array,
• The similar-looking In the pointer
version,
In the pointer char *date = "June 14"; date points
version, date
declares date to be a pointer. to a string
is a variable literal that
that can point shouldn’t be
to other modified.
strings.
15 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Character Arrays versus Character Pointers


• The declaration
char *p;
does not allocate space for a string.
• Before we can use p as a string, it must point to an
array of characters.
• One possibility is to make p point to a string variable:
char str[STR_LEN+1], *p;
p = str;
• Another possibility is to make p point to a
dynamically allocated string.
16 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Character Arrays versus Character Pointers


• Using an uninitialized pointer variable as a
string is a serious error.
• An attempt at building the string "abc":
char *p;
p[0] = 'a'; /*** WRONG ***/
p[1] = 'b'; /*** WRONG ***/
p[2] = 'c'; /*** WRONG ***/
p[3] = '\0'; /*** WRONG ***/
• Since p hasn’t been initialized, this causes
undefined behavior.
17 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Reading and Writing Strings


• Writing a string is easy using either printf or
puts.
• Reading a string is a bit harder, because the input
may be longer than the string variable into which
it’s being stored.
• To read a string in a single step, we can use either
scanf or gets.
• As an alternative, we can read strings one
character at a time.

18 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Writing Strings Using printf and puts


• The %s conversion specification allows printf
to write a string:
char str[] = "Are we having fun yet?";
printf("%s\n", str);
The output will be
Are we having fun yet?

• printf writes the characters in a string one by


one until it encounters a null character.

19 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Writing Strings Using printf and puts


• To print part of a string, use the conversion
specification %.ps.
• p is the number of characters to be displayed.
• The statement
printf("%.6s\n", str);
will print
Are we

20 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Writing Strings Using printf and puts


• The %ms conversion will display a string in a field
of size m.
• If the string has fewer than m characters, it will be
right-justified within the field.
• To force left justification instead, we can put a
minus sign in front of m.
• The statement
printf("%25s\n", str);
will print Are we having fun yet?

21 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Writing Strings Using printf and puts


• The m and p values can be used in combination.
• A conversion specification of the form %m.ps
causes the first p characters of a string to be
displayed in a field of size m.
printf("%25.6s\n", str);
Are we

printf("%-25.6s\n", str);
Are we

22 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Writing Strings Using printf and puts


• printf isn’t the only function that can write
strings.
• The C library also provides puts:
puts(str);
• After writing a string, puts always writes an
additional new-line character.
Are we having fun yet?

23 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Reading Strings Using scanf and gets


• The %s conversion specification allows scanf to
read a string into a character array:
scanf("%s", str);
• str is treated as a pointer, so there’s no need to
put the & operator in front of str.
• When scanf is called, it skips white space, then
reads characters and stores them in str until it
encounters a white-space character.
• scanf always stores a null character at the end
of the string.
24 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Reading Strings Using scanf and gets


• scanf won’t usually read a full line of input.
• A new-line character will cause scanf to stop
reading, but so will a space or tab character.
• To read an entire line of input, we can use gets.
• Properties of gets:
– Doesn’t skip white space before starting to read input.
– Reads until it finds a new-line character.
– Discards the new-line character instead of storing it; the
null character takes its place.

25 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Reading Strings Using scanf and gets


• Consider the following program fragment:
char sentence[SENT_LEN+1];
printf("Enter a sentence:\n");
scanf("%s", sentence);
• Suppose that after the prompt
Enter a sentence:
the user enters the line
To C, or not to C: that is the question.
• scanf will store the string "To" in sentence.

26 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Reading Strings Using scanf and gets


• Suppose that we replace scanf by gets:
gets(sentence);
• When the user enters the same input as before,
gets will store the string
" To C, or not to C: that is the question."
in sentence.

27 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Reading Strings Character by Character


• Suppose we need a function that (1) doesn’t skip
white-space characters, (2) stops reading at the first
new-line character (which isn’t stored in the string),
and (3) discards extra characters.
• A prototype for the function:
int read_line(char str[], int n);
• If the input line contains more than n characters,
read_line will discard the additional characters.
• read_line will return the number of characters it
stores in str.

28 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Reading Strings Character by Character


• read_line consists primarily of a loop that calls
getchar to read a character and then stores the character
in str, provided that there’s room left: ch has int type
int read_line(char str[], int n) rather than char
{ type because
int ch, i = 0; getchar returns an
int value.
while ((ch = getchar()) != '\n')
if (i < n)
str[i++] = ch;
str[i] = '\0'; /* terminates string */
return i; /* number of characters stored */
} put a null character at the
end of an input string.
29 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Accessing the Characters in a String


• Since strings are stored as arrays, we can use
subscripting to access the characters in a string.
• A function that counts the number of spaces in a
string:
int count_spaces(const char s[])
{ To process every
int count = 0, i; character in a string s,
we can set up a loop
for (i = 0; s[i] != '\0'; i++) that increments a
if (s[i] == ' ') counter i and selects
count++; characters via the
expression s[i].
return count;
}
30 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Accessing the Characters in a String


• A version that uses pointer arithmetic instead of
array subscripting :
int count_spaces(const char *s)
{
int count = 0;
for (; *s != '\0'; s++)
if (*s == ' ')
count++;
return count;
}

31 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Using the C String Library


• Some programming languages provide operators
that can copy strings, compare strings, concatenate
strings, select substrings, and the like.
• C’s operators, in contrast, are essentially useless
for working with strings.
• Strings are treated as arrays in C, so they’re
restricted in the same ways as arrays.
• In particular, they can’t be copied or compared
using operators.

32 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Using the C String Library


• Direct attempts to copy or compare strings will fail.
• Copying a string into a character array using the =
operator is not possible:
char str1[10], str2[10];

str1 = "abc"; /*** WRONG ***/
str2 = str1; /*** WRONG ***/
Using an array name as the left operand of = is illegal.
• Initializing a character array using = is legal, though:
char str1[10] = "abc";
In this context, = is not the assignment operator.
33 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

Using the C String Library


• Attempting to compare strings using a relational
or equality operator is legal but won’t produce
the desired result:
if (str1 == str2) … /*** WRONG ***/
• This statement compares str1 and str2 as
pointers.
• Since str1 and str2 have different addresses,
the expression str1 == str2 must have the
value 0.

34 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Using the C String Library


• The C library provides a rich set of functions for
performing operations on strings.
• Programs that need string operations should
contain the following line:
#include <string.h>
• In subsequent examples, assume that str1 and
str2 are character arrays used as strings.
char str1[10], str2[10];

35 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcpy (String Copy) Function


• Prototype for the strcpy function:
char *strcpy(char *s1, const char *s2);
• strcpy copies the string s2 into the string s1.
– To be precise, we should say “strcpy copies the
string pointed to by s2 into the array pointed to by
s1.”
• strcpy returns s1 (a pointer to the destination
string).

36 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcpy (String Copy) Function


• A call of strcpy that stores the string "abcd"
in str2:
strcpy(str2, "abcd");
str2 a b c d \0 \0 \0 \0 \0 \0

• A call that copies the contents of str2 into


str1:
strcpy(str1, str2);

str1 a b c d \0 \0 \0 \0 \0 \0

37 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcpy (String Copy) Function


• Calling the strncpy function is a safer, albeit
slower, way to copy a string.
• strncpy has a third argument that limits the
number of characters that will be copied.
• A call of strncpy that copies str2 into str1:
strncpy(str1, str2, sizeof(str1));
• A safer way to use strncpy:
strncpy(str1, str2, sizeof(str1) - 1);
str1[sizeof(str1)-1] = '\0';

38 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strlen (String Length) Function


• Prototype for the strlen function:
size_t strlen(const char *s);
• size_t is a typedef name that represents one of C’s
unsigned integer types.
• strlen returns the length of a string s, not including the
null character.
• Examples:
int len;
len = strlen("abc"); /* len is now 3 */
len = strlen(""); /* len is now 0 */
strcpy(str1, "abc");
len = strlen(str1); /* len is now 3 */

39 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcat (String Concatenation) Function


• Prototype for the strcat function:
char *strcat(char *s1, const char *s2);
• strcat appends the contents of the string s2 to
the end of the string s1.
• It returns s1 (a pointer to the resulting string).

40 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcat (String Concatenation) Function


• strcat examples:
strcpy(str1, "abc");
str1 a b c \0 \0 \0 \0 \0 \0 \0

strcat(str1, "def");
str1 a b c d e f \0 \0 \0 \0

/* str1 now contains "abcdef" */

41 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcat (String Concatenation) Function


• strcat examples:
strcpy(str1, "abc");
str1 a b c \0 \0 \0 \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

/* str1 now contains "abcdef" */


42 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

The strcat (String Concatenation) Function


strcpy(str1, "abc");
str1 a b c \0 \0 \0 \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

43 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcat (String Concatenation) Function


• strcat(str1, str2) causes undefined
behavior if the str1 array isn’t long enough to
accommodate the characters from str2.
• Example:
char str1[6] = "abc";

str1 a b c \0 \0 \0

strcat(str1, "def"); /*** WRONG ***/


• str1 is limited to six characters, causing
strcat to write past the end of the array.
44 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 13: Strings

The strcat (String Concatenation) Function


• The strncat function is a safer but slower
version of strcat.
• Like strncpy, it has a third argument that
limits the number of characters it will copy.
• A call of strncat:
strncat(str1, str2, sizeof(str1) - strlen(str1) - 1);
• strncat will terminate str1 with a null
character, which isn’t included in the third
argument.

45 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcmp (String Comparison) Function


• Prototype for the strcmp function:
int strcmp(const char *s1, const char *s2);
• strcmp compares the strings s1 and s2,
returning a value less than, equal to, or greater
than 0, depending on whether s1 is less than,
equal to, or greater than s2.

46 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcmp (String Comparison) Function


• Testing whether str1 is less than str2:
if (strcmp(str1, str2) < 0) /* is str1 < str2? */

• Testing whether str1 is less than or equal to
str2:
if (strcmp(str1, str2) <= 0) /* is str1 <= str2? */

• By choosing the proper operator (<, <=, >, >=,
==, !=), we can test any possible relationship
between str1 and str2.

47 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcmp (String Comparison) Function


• strcmp considers s1 to be less than s2 if either
one of the following conditions is satisfied:
– The first i characters of s1 and s2 match, but the
(i+1)st character of s1 is less than the (i+1)st character
of s2.
– All characters of s1 match s2, but s1 is shorter than
s2.

48 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

The strcmp (String Comparison) Function


• As it compares two strings, strcmp looks at the
numerical codes for the characters in the strings.
• Some knowledge of the underlying character set is
helpful to predict what strcmp will do.
• Important properties of ASCII:
– A–Z, a–z, and 0–9 have consecutive codes.
– All upper-case letters are less than all lower-case
letters.
– Digits are less than letters.
– Spaces are less than all printing characters.

49 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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"};

• The number of rows in the array can be omitted,


but we must specify the number of columns.

50 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Arrays of Strings
• Unfortunately, the planets array contains a fair
bit of wasted space (extra null characters):

51 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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"};

52 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

Arrays of Strings
• This small change has a dramatic effect on how
planets is stored:

53 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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]);

54 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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

55 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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.

56 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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.

57 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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:

58 Copyright © 2008 W. W. Norton & Company.


All rights reserved.
Chapter 13: Strings

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);

60 Copyright © 2008 W. W. Norton & Company.


All rights reserved.

You might also like