Bits,
Characters,
C-Strings and
structs
OBJECTIVES
In this chapter you will learn:
Hilaire Belloc
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Self-Review Exercises
22.1
22.2
State whether each of the following is true or false. If false, explain why.
a) Structures may contain only one data type.
ANS: False. A structure can contain many data types.
b) Members of different structures must have unique names.
ANS: False. The members of separate structures can have the same names, but the members
of the same structure must have unique names.
c) Keyword typedef is used to define new data types.
ANS: False. typedef is used to define aliases for previously defined data types.
d) Structures are always passed to functions by reference.
ANS: False. Structures are passed to functions by value by default and may be passed by
reference.
22.3
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Self-Review Exercises
10 ]
to be of
ANS: Part a;
Part b[ 10 ];
Part *ptr;
d) Read a part number and a part name from the keyboard into the members of variable a.
ANS: cin >> a.partNumber >> a.partName;
g) Print the member values of element three of array b, using the variable ptr and the structure pointer operator to refer to the members.
ANS: cout << ( ptr + 3 )->partNumber << ' '
<< ( ptr + 3 )->partName << endl;
22.4
ANS: Error: The parentheses that should enclose *cPtr have been omitted, causing the or-
ANS: Error: The array subscript has been omitted. The expression should be
c)
hearts[ 10 ].face.
struct Person
{
char lastName[ 15 ];
char firstName[ 15 ];
int age;
}
has been
p = c;
ANS: Error: Variables of different structure types cannot be assigned to one another.
22.5 Write a single statement to accomplish each of the following. Assume that variables c
(which stores a character), x, y and z are of type int; variables d, e and f are of type double; variable
ptr is of type char * and arrays s1[ 100 ] and s2[ 100 ] are of type char.
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
ANS: c = toupper( c );
b) Determine if the value of variable c is a digit. Use the conditional operator as shown in
Figs. 22.1822.20 to print " is a " or " is not a " when the result is displayed.
ANS: cout << '\'' << c << "\' "
<< ( isdigit( c ) ? "is a" : "is not a" )
<< " digit" << endl;
d) Determine whether the value of variable c is a control character. Use the conditional
operator to print " is a " or " is not a " when the result is displayed.
ANS: cout << '\'' << c << "\' "
<< ( iscntrl( c ) ? "is a" : "is not a" )
g) Determine whether the value of c is a letter. Use the conditional operator to print " is
a " or " is not a " when the result is displayed.
i) Determine whether the value of variable c is a printing character. Use the conditional
operator to print " is a " or " is not a " when the result is displayed.
ANS: cout << '\'' << c << "\' "
<< ( isprint( c ) ? "is a" : "is not a" )
<< " printing character" << endl;
j) Assign to ptr the location of the first occurrence in s1 of any character from s2.
ANS: ptr = strpbrk( s1, s2 );
Exercises
22.6
30 ],
integer partNumber,
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
int stock;
int reorder;
};
25 ], city[
15 ] and variable
d) Structure Test, containing 16 bit fields with widths of 1 bit. The names of the bit fields
are the letters a to p.
ANS: struct Test
{
unsigned a:1, b:1, c:1, d:1, e:1, f:1, g:1, h:1,
i:1, j:1, k:1, l:1, m:1, n:1, o:1, p:1;
};
22.7
Write a separate expression that accesses the structure members in each of the following parts:
a) Member lastName of structure customerRecord.
ANS: customerRecord.lastName
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
22.8 Modify the program of Fig. 22.14 to shuffle the cards using a high-performance shuffle, as
shown in Fig. 22.3. Print the resulting deck in two-column format, as in Fig. 22.4. Precede each
card with its color.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
8
51
52
53
54
55
56
57
1
2
3
4
5
6
7
8
9
10
11
cout << right << setw( 5 ) << color[ deck[ i ].color ] << ": "
<< setw( 8 ) << face[ deck[ i ].face ] << " of "
<< left << setw( 8 ) << suit[ deck[ i ].suit ]
<< ( ( i + 1 ) % 2 ? '\t' : '\n' );
} // end for
} // end function deal
Black:
Red:
Red:
Red:
Black:
Red:
Red:
Red:
Red:
Red:
Black:
Black:
Red:
Red:
Black:
Black:
Black:
Red:
Red:
Black:
Black:
Black:
Black:
Red:
Red:
Red:
Three
Ten
Five
King
Ace
Seven
Jack
King
Queen
Deuce
Nine
Four
Four
Eight
Queen
Ace
Deuce
Three
Nine
Jack
King
Five
Seven
Queen
Six
Ace
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
Spades
Diamonds
Diamonds
Diamonds
Clubs
Diamonds
Hearts
Hearts
Hearts
Diamonds
Spades
Spades
Hearts
Hearts
Clubs
Spades
Spades
Diamonds
Diamonds
Clubs
Spades
Spades
Spades
Diamonds
Diamonds
Hearts
Red:
Red:
Red:
Black:
Red:
Red:
Black:
Black:
Black:
Black:
Black:
Black:
Black:
Black:
Red:
Red:
Black:
Black:
Red:
Black:
Red:
Black:
Red:
Red:
Black:
Black:
Nine
Ten
Eight
Five
Five
Ace
Six
Queen
Ten
Three
Six
Jack
King
Ten
Four
Seven
Eight
Deuce
Deuce
Four
Six
Nine
Three
Jack
Eight
Seven
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
of
Hearts
Hearts
Diamonds
Clubs
Hearts
Diamonds
Clubs
Spades
Clubs
Clubs
Spades
Spades
Clubs
Spades
Diamonds
Hearts
Spades
Clubs
Hearts
Clubs
Hearts
Clubs
Hearts
Diamonds
Clubs
Clubs
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
22.9 Write a program that right-shifts an integer variable 4 bits. The program should print the
integer in bits before and after the shift operation. Does your system place zeros or ones in the vacated bits?
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
10
22.10 Left-shifting an unsigned integer by 1 bit is equivalent to multiplying the value by 2. Write
function power2 that takes two integer arguments, number and pow, and calculates
number * 2pow
Use a shift operator to calculate the result. The program should print the values as integers and as bits.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
11
4 * 2^7 = 512
512 = 00000000 00000000 00000010 00000000
22.11 The left-shift operator can be used to pack two character values into a two-byte unsigned
integer variable. Write a program that inputs two characters from the keyboard and passes them to
function packCharacters. To pack two characters into an unsigned integer variable, assign the first
character to the unsigned variable, shift the unsigned variable left by 8 bit positions and combine
the unsigned variable with the second character using the bitwise inclusive-OR operator. The program should output the characters in their bit format before and after they are packed into the unsigned integer to prove that they are in fact packed correctly in the unsigned variable.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
12
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
char a;
char b;
unsigned result;
cout << "Enter two characters: ";
cin >> a >> b;
cout << "\n\'" << a << '\'' << " in bits as an unsigned integer is:\n";
displayBits( a );
cout << '\'' << b << '\'' << " in bits as an unsigned integer is:\n";
displayBits( b );
result = packCharacters( a, b );
cout << "\n\'" << a << '\'' << " and " << '\'' << b << '\''
<< " packed in an unsigned integer:\n";
displayBits( result );
return 0;
} // end main
// pack two character value into unsigned integer
unsigned packCharacters( char x, char y )
{
unsigned pack = x;
pack <<= 8; // left shift assignment operator
pack |= y; // bitwise exclusive-OR operator
return pack;
} // end function packCharacters
// display the bits in value
void displayBits( unsigned value )
{
const int SHIFT = 8 * sizeof( unsigned ) - 1;
const unsigned MASK = 1 << SHIFT;
cout << setw( 7 ) << value << " = ";
for ( unsigned c = 1; c <= SHIFT + 1; c++ )
{
cout << ( value & MASK ? '1' : '0' );
value <<= 1;
if ( c % 8 == 0 ) // output a space after 8 bits
cout << ' ';
} // end for
cout << endl;
} // end function displayBits
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
13
22.12 Using the right-shift operator, the bitwise AND operator and a mask, write function unpackCharacters that takes the unsigned integer from Exercise 22.11 and unpacks it into two characters. To unpack two characters from an unsigned two-byte integer, combine the unsigned integer
with the mask 65280 (11111111 00000000) and right-shift the result 8 bits. Assign the resulting value
to a char variable. Then, combine the unsigned integer with the mask 255 (00000000 11111111).
Assign the result to another char variable. The program should print the unsigned integer in bits
before it is unpacked, then print the characters in bits to confirm that they were unpacked correctly.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
14
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
unsigned pack )
unsigned mask1 = 65280;
unsigned mask2 = 255;
22.13 If your system uses four-byte integers, rewrite the program of Exercise 22.11 to pack four
characters.
22.14 If your system uses four-byte integers, rewrite the function unpackCharacters of
Exercise 22.12 to unpack four characters. Create the masks you need to unpack the 4 characters by
left-shifting the value 255 in the mask variable by 8 bits 0, 1, 2 or 3 times (depending on the byte
you are unpacking).
22.15 Write a program that reverses the order of the bits in an unsigned integer value. The program should input the value from the user and call function reverseBits to print the bits in reverse
order. Print the value in bits both before and after the bits are reversed to confirm that the bits are
reversed properly.
ANS:
1
2
3
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using std::endl;
using std::cin;
#include <iomanip>
using std::setw;
// prototypes
void reverseBits( unsigned * const );
void displayBits( unsigned );
int main()
{
unsigned a;
cout << "Enter an unsigned integer: ";
cin >> a;
// before call to reverseBits
cout << "\nBefore bits are reversed:\n";
displayBits( a );
// after call to reverseBits
reverseBits( &a );
cout << "\nAfter bits are reversed:\n";
displayBits( a );
return 0;
} // end main
// reverse the bits
void reverseBits( unsigned * const vPtr )
{
const int SHIFT = 8 * sizeof( unsigned ) - 1;
const unsigned MASK = 1;
unsignedvalue = *vPtr;
for ( int i = 0; i <= 15; i++ )
{
*vPtr <<= 1; // left shift the pointer
*vPtr |= ( value & MASK ); // inclusive-OR operator
value >>= 1; // right shift
} // end for
} // end function reverseBits
// display the bits
void displayBits( unsigned value )
{
const int SHIFT = 8 * sizeof( unsigned ) - 1;
const unsigned MASK = 1 << SHIFT;
cout << setw( 7 ) << value << " = ";
for ( unsigned c = 1; c <= SHIFT + 1; c++ )
{
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
15
16
58
59
60
61
62
63
64
65
66
22.16 Write a program that demonstrates passing an array by value. [Hint: Use a struct.] Prove
that a copy was passed by modifying the array copy in the called function.
22.17 Write a program that inputs a character from the keyboard and tests the character with each
function in the character-handling library. Print the value returned by each function.
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
17
22.18 The following program uses function multiple to determine whether the integer entered
from the keyboard is a multiple of some integer X. Examine function multiple, then determine the
value of X.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
18
22.20 Write a program that inputs a line of text with istream member function getline (as in
Chapter 15) into character array s[ 100 ]. Output the line in uppercase letters and lowercase letters.
ANS:
1
2
3
4
5
6
7
8
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
19
using std::tolower;
const int SIZE = 100;
int main()
{
char s[ SIZE ];
int i;
cout << "Enter a line of text:\n";
cin.getline( s, SIZE );
cout << "\nThe line in uppercase is:\n";
// demonstrate function toupper
for ( i = 0; s[ i ] != '\0'; ++i )
cout << static_cast< char >( toupper( s[ i ] ) );
cout << "\n\nThe line in lowercase is:\n";
// demonstrate function tolower
for ( i = 0; s[ i ] != '\0'; ++i )
cout << static_cast< char >( tolower( s[ i ] ) );
cout << endl;
return 0;
} // end main
22.21 Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. Use only the C-style string-processing
techniques shown in this chapter.
ANS:
1
2
3
4
5
6
7
8
9
10
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
20
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
char stringValue[ SIZE ];
int sum = 0;
for ( int i = 1; i <= 4; ++i )
{
cout << "Enter an integer string: ";
cin >> stringValue;
sum += atoi( stringValue ); // add converted stringValue to sum
} // end for
cout << "The total of the values is " << sum << endl;
return 0;
} // end main
Enter an integer
Enter an integer
Enter an integer
Enter an integer
The total of the
string: 11
string: 22
string: 44
string: 88
values is 165
22.22 Write a program that inputs four strings that represent floating-point values, converts the
strings to double values, sums the values and prints the total of the four values. Use only the C-style
string-processing techniques shown in this chapter.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
25
26
27
28
29
30
31
32
33
21
Enter
Enter
Enter
Enter
a
a
a
a
floating-point
floating-point
floating-point
floating-point
value:
value:
value:
value:
4.3
6.4
2.4
7.2
22.23 Write a program that inputs a line of text and a search string from the keyboard. Using
function strstr, locate the first occurrence of the search string in the line of text, and assign the
location to variable searchPtr of type char *. If the search string is found, print the remainder of
the line of text beginning with the search string. Then use strstr again to locate the next occurrence
of the search string in the line of text. If a second occurrence is found, print the remainder of the
line of text beginning with the second occurrence. [Hint: The second call to strstr should contain
the expression searchPtr + 1 as its first argument.]
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
22
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
22.24 Write a program based on the program of Exercise 22.23 that inputs several lines of text and
a search string, then uses function strstr to determine the total number of occurrences of the string
in the lines of text. Print the result.
ANS:
1
2
3
4
5
6
7
8
9
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <cctype>
using std::tolower;
const int SIZE1 = 80;
const int SIZE2 = 20;
int main()
{
char text[ 3 ][ SIZE1 ];
char search[ SIZE2 ];
char *searchPtr;
int count = 0;
int i;
cout << "Enter three lines of text:\n";
for ( i = 0; i <= 2; i++ )
cin.getline( &text[ i ][ 0 ], SIZE1 );
// make all characters lowercase
for ( i = 0; i <= 2; i++ )
for ( int j = 0; text[ i ][ j ] != '\0'; j++ )
{
char c = static_cast< char >( tolower( text[ i ][ j ] ) );
text[ i ][ j ] = c;
} // end for
cout << "\nEnter a search string: ";
cin >> search;
for ( i = 0; i <= 2; i++ )
{
searchPtr = &text[ i ][ 0 ];
while ( searchPtr = strstr( searchPtr, search ) )
{
count++;
searchPtr++;
} // end while
} // end for
cout << "\nThe total number of occurrences of \"" << search
<< "\" in the text is: " << count << endl;
return 0;
} // end main
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
23
24
22.25 Write a program that inputs several lines of text and a search character and uses function
strchr to determine the total number of occurrences of the character in the lines of text.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
25
22.26 Write a program based on the program of Exercise 22.25 that inputs several lines of text
and uses function strchr to determine the total number of occurrences of each letter of the alphabet
in the text. Uppercase and lowercase letters should be counted together. Store the totals for each
letter in an array, and print the values in tabular format after the totals have been determined.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
26
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
27
22.27 The chart in Appendix B shows the numeric code representations for the characters in the
ASCII character set. Study this chart, and then state whether each of the following is true or false:
a) The letter A comes before the letter B.
ANS: True.
b) The digit 9 comes before the digit 0.
ANS: False.
c) The commonly used symbols for addition, subtraction, multiplication and division all
come before any of the digits.
ANS: True.
d) The digits come before the letters.
ANS: True.
e) If a sort program sorts strings into ascending sequence, then the program will place the
symbol for a right parenthesis before the symbol for a left parenthesis.
ANS: False.
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
28
22.28 Write a program that reads a series of strings and prints only those strings beginning with
the letter b.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Enter
Enter
Enter
Enter
Enter
a
a
a
a
a
string:
string:
string:
string:
string:
apples
bananas
blueberries
tomatoes
bread
22.29 Write a program that reads a series of strings and prints only those strings that end with the
letters ED.
ANS:
1
2
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using std::cout;
using std::cin;
#include <cstring>
using std::strlen;
using std::strcmp;
const int SIZE = 20;
int main()
{
int length, i;
char array[ 5 ][ SIZE ];
// takes five strings and store into character array
for ( i = 0; i <= 4; i++ )
{
cout << "Enter a string: ";
cin.getline( &array[ i ][ 0 ], SIZE );
} // end for
cout << "\nThe strings ending with \"ED\" are:\n";
// review each string
for ( i = 0; i <= 4; i++ )
{
length = strlen( &array[ i ][ 0 ] );
// check if string ends with "ED"
if ( strcmp( &array[ i ][ length - 2 ], "ED" ) == 0 )
cout << &array[ i ][ 0 ] << '\n';
} // end for
return 0;
} // end main
Enter
Enter
Enter
Enter
Enter
a
a
a
a
a
string:
string:
string:
string:
string:
MOVED
SAW
RAN
CARVED
PROVED
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
29
30
22.30 Write a program that inputs an ASCII code and prints the corresponding character. Modify
this program so that it generates all possible three-digit codes in the range 000255 and attempts to
print the corresponding characters. What happens when this program is run?
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
on screen.]
1
2
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
31
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
int main()
{
for ( int c = 0; c <= 255; c++ )
{
if ( c % 7 == 0 )
cout << '\n';
cout << setw( 6 ) << c << setw( 3 ) << static_cast< char > ( c );
} // end for
cout << endl;
return 0;
} // end main
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
32
0
7
11
14
21
28
35
42
49
56
63
70
77
84
91
98
105
112
119
126
133
140
147
154
161
168
175
182
189
196
203
210
217
224
231
238
245
252
1
8
12
15
22
29
36
43
50
57
64
71
78
85
92
99
106
113
120
127
134
141
148
155
162
169
176
183
190
197
204
211
218
225
232
239
246
253
?
?
?
#
*
1
8
?
F
M
T
[
b
i
p
w
~
+
+
a
t
e
)
n
?
?
?
?
$
+
2
9
@
G
N
U
\
c
j
q
x
+
+
+
+
+
F
n
13
16
23
30
37
44
51
58
65
72
79
86
93
100
107
114
121
128
135
142
149
156
163
170
177
184
191
198
205
212
219
226
233
240
247
254
?
?
?
%
,
3
:
A
H
O
V
]
d
k
r
y
+
+
G
T
=
17
24
31
38
45
52
59
66
73
80
87
94
101
108
115
122
129
136
143
150
157
164
171
178
185
192
199
206
213
220
227
234
241
248
255
?
?
?
&
4
;
B
I
P
W
^
e
l
s
z
+
+
_
p
O
18 ?
25 ?
32
39 '
46 .
53 5
60 <
67 C
74 J
81 Q
88 X
95 _
102 f
109 m
116 t
123 {
130
137
144
151
158 P
165
172
179
186
193 200 +
207 214 +
221
228 S
235 d
242 =
249
10
19 ?
26 ?
33 !
40 (
47 /
54 6
61 =
68 D
75 K
82 R
89 Y
96 `
103 g
110 n
117 u
124 |
131
138
145
152
159
166
173
180
187 +
194 201 +
208 215 +
222
229 s
236 8
243 =
250
20
27 ?
34 "
41 )
48 0
55 7
62 >
69 E
76 L
83 S
90 Z
97 a
104 h
111 o
118 v
125 }
132
139
146
153
160
167
174
181
188 +
195 +
202 209 216 +
223
230
237 f
244 (
251 v
22.31 Using the ASCII character chart in Appendix B as a guide, write your own versions of the
character-handling functions in Fig. 22.17.
ANS:
1
2
3
4
5
6
7
8
9
10
11
);
);
);
);
);
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int
int
int
int
int
int
isSpace(
isPunct(
isPrint(
isGraph(
toLower(
toUpper(
int
int
int
int
int
int
33
);
);
);
);
);
);
int main()
{
int v;
char a; // hold a character
char header[] = "According to";
char *names[] = { "isDigit ", "isAlpha ", "isAlNum ", "isLower ",
"isUpper ", "isSpace ", "isPunct ", "isPrint ", "isGraph ",
"toLower ", "toUpper " };
char *names2[] = { "digit", "letter", "letter/digit", "lowercase",
"uppercase", "space", "punctuation", "print", "graph",
"converted lowercase", "converted uppercase" };
int ( *f[] )( int ) = { isDigit, isAlpha, isAlNum, isLower,
isUpper, isSpace, isPunct, isPrint,
isGraph, toLower, toUpper };
cout << "Enter a character: ";
cin >> a;
for ( int k = 0; k < 11; ++k )
{
v = ( *f[ k ] )( static_cast< int >( a ) );
cout.write( header, 13 );
cout << names[ k ] << a << ( !v ? " is not a " : " is a " )
<< names2[ k ] << " character\n";
} // end for
return 0;
} // end main
// Normally these would return bool, but int return type
// is consistent with the ctype library.
// determine whether c is an integer
int isDigit( int c )
{
return ( c >= 48 && c <= 57 ) ? 1 : 0;
} // end function isDigit
// return true if c is a letter
int isAlpha( int c )
{
return ( ( c >= 65 && c <= 90 ) || ( c >= 97 && c <= 122 ) ) ? 1 : 0;
} // end function is Alpha
// return true if c is a digit
int isAlNum( int c )
{
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
34
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
Enter a character: A
According to isDigit,
According to isAlpha,
According to isAlNum,
According to isLower,
According to isUpper,
According to isSpace,
According to isPunct,
According to isPrint,
According to isGraph,
According to toLower,
According to toUpper,
A
A
A
A
A
A
A
A
A
A
A
is
is
is
is
is
is
is
is
is
is
is
Enter a character: 4
According to isDigit,
According to isAlpha,
According to isAlNum,
According to isLower,
According to isUpper,
According to isSpace,
According to isPunct,
According to isPrint,
According to isGraph,
According to toLower,
According to toUpper,
4
4
4
4
4
4
4
4
4
4
4
is
is
is
is
is
is
is
is
is
is
is
a digit character
not a letter character
a letter/digit character
not a lowercase character
not a uppercase character
not a space character
not a punctuation character
a print character
a graph character
a converted lowercase character
a converted uppercase character
35
22.32 Write your own versions of the functions in Fig. 22.21 for converting strings to numbers.
22.33 Write your own versions of the functions in Fig. 22.28 for searching strings.
22.34 Write your own versions of the functions in Fig. 22.35 for manipulating blocks of memory.
22.35 (Project: A Spelling Checker) Many popular word-processing software packages have builtin spell checkers. We used spell-checking capabilities in preparing this book and discovered that, no
matter how careful we thought we were in writing a chapter, the software was always able to find a
few more spelling errors than we were able to catch manually.
In this project, you are asked to develop your own spell-checker utility. We make suggestions
to help get you started. You should then consider adding more capabilities. You might nd it helpful to use a computerized dictionary as a source of words.
Why do we type so many words with incorrect spellings? In some cases, it is because we simply do not know the correct spelling, so we make a best guess. In some cases, it is because we
transpose two letters (e.g., defualt instead of default). Sometimes we double-type a letter accidentally (e.g., hanndy instead of handy). Sometimes we type a nearby key instead of the one we
intended (e.g., biryhday instead of birthday). And so on.
Design and implement a spell-checker program. Your program maintains an array wordList of
character strings. You can either enter these strings or obtain them from a computerized dictionary.
Your program asks a user to enter a word. The program then looks up that word in the
wordList array. If the word is present in the array, your program should print Word is spelled
correctly.
If the word is not present in the array, your program should print Word is not spelled correctly. Then your program should try to locate other words in wordList that might be the word
the user intended to type. For example, you can try all possible single transpositions of adjacent let-
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
36
ters to discover that the word default is a direct match to a word in wordList. Of course, this
implies that your program will check all other single transpositions, such as edfault, dfeault,
deafult, defalut and defautl. When you nd a new word that matches one in wordList, print
that word in a message such as Did you mean "default?".
Implement other tests, such as the replacing of each double letter with a single letter and any
other tests you can develop to improve the value of your spell checker.
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.