Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
Solutions
to selected exercises can be found in the electronic document The Thinking
in C++ Volume 2 Annotated Solution Guide, available for a small fee from www.MindView.net.
1. Write and test a function that reverses the order of the
characters in a string.
2. A palindrome is a word or group of words that read the same
forward and backward. For example madam or wow. Write a program that takes
a string argument from the command line and, using the function from the
previous exercise, prints whether the string was a palindrome or not.
3. Make your program from Exercise 2 return true even if
symmetric letters differ in case. For example, Civic would still return true
although the first letter is capitalized.
4. Change your program from Exercise 3 to ignore punctuation and
spaces as well. For example Able was I, ere I saw Elba. would report true.
5. Using the following string declarations and only chars (no
string literals or magic numbers):
string one("I walked down the canyon with the
moving mountain bikers.");
string
two("The bikers passed by me too close for comfort.");
string three("I went hiking instead.");
produce the following sentence:
I
moved down the canyon with the mountain bikers. The mountain bikers passed by
me too close for comfort. So I went hiking instead.
6. Write a program named replace that takes three
command-line arguments representing an input text file, a string to replace
(call it from), and a replacement string (call it to). The
program should write a new file to standard output with all occurrences of from
replaced by to.
7. Repeat the previous exercise but replace all instances of from
regardless of case.
8. Make your program from Exercise 3 take a filename from the command-line,
and then display all words that are palindromes (ignoring case) in the file. Do
not display duplicates (even if their case differs). Do not try to look for
palindromes that are larger than a word (unlike in Exercise 4).
9. Modify HTMLStripper.cpp so that when it encounters a tag,
it displays the tag s name, then displays the file s contents between the tag
and the file s ending tag. Assume no nesting of tags, and that all tags have
ending tags (denoted with </TAGNAME>).
10. Write a program that takes three command-line arguments (a
filename and two strings) and displays to the console all lines in the file
that have both strings in the line, either string, only one string, or neither
string, based on user input at the beginning of the program (the user will
choose which matching mode to use). For all but the neither string option,
highlight the input string(s) by placing an asterisk (*) at the beginning and
end of each string s occurrence when it is displayed.
11. Write a program that takes two command-line arguments (a filename
and a string) and counts the number of times the string occurs in the file,
even as a substring (but ignoring overlaps). For example, an input string of ba
would match twice in the word basketball, but an input string of ana would
match only once in the word banana. Display to the console the number of
times the string is matched in the file, as well as the average length of the
words where the string occurred. (If the string occurs more than once in a
word, only count the word once in figuring the average.)
12. Write a program that takes a filename from the command line and
profiles the character usage, including punctuation and spaces (all character
values of 0x21 [33] through 0x7E [126], as well as the space character). That is,
count the number of occurrences of each character in the file, then display the
results sorted either sequentially (space, then !, ", #, etc.) or by
ascending or descending frequency based on user input at the beginning of the
program. For space, display the word Space instead of the character ' '. A
sample run might look something like this:
Format sequentially, ascending, or descending
(S/A/D): D
t: 526
r: 490
etc.
13. Using find( ) and rfind( ), write a
program that takes two command-line arguments (a filename and a string) and
displays the first and last words (and their indexes) not matching the string,
as well as the indexes of the first and last instances of the string. Display Not
Found if any of the searches fail.
14. Using the find_first_of family of functions (but not
exclusively), write a program that will remove all non-alphanumeric characters
except spaces and periods from a file, then capitalize the first letter
following a period.
15. Again using the find_first_of family of functions, write
a program that accepts a filename as a command-line argument and then formats
all numbers in the file to currency. Ignore decimal points after the first
until a non-numeric character is found, and round to the nearest hundredth. For
example, the string 12.399abc29.00.6a would be formatted (in the USA) to
$12.40abc$29.01a.
16. Write a program that accepts two command-line arguments (a
filename and a number) and scrambles each word in the file by randomly
switching two of its letters the number of times specified in the second
argument. (That is, if 0 is passed into your program from the command-line, the
words should not be scrambled; if 1 is passed in, one pair of randomly-chosen
letters should be swapped, for an input of 2, two random pairs should be
swapped, etc.).
17. Write a program that accepts a filename from the command line and
displays the number of sentences (defined as the number of periods in the
file), average number of characters per sentence, and the total number of
characters in the file.
18. Prove to yourself that the at( ) member function
really will throw an exception if an attempt is made to go out of bounds, and
that the indexing operator ([ ]) won t.