Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Devise a program that counts the number of characters in its input up to the end of file.

  2. Write a program that reads input until encountering EOF . Have the program print each input character and its ASCII decimal value. Note that characters preceding the space character in the ASCII sequence are nonprinting characters. Treat them specially. If the nonprinting character is a newline or tab, print \n or \t , respectively. Otherwise, use control-character notation. For instance, ASCII 1 is Ctrl+A, which can be displayed as ^A . Note that the ASCII value for A is the value for Ctrl+A plus 64. A similar relation holds for the other nonprinting characters. Print ten pairs per line, but start a fresh line each time a newline character is encountered .

  3. Write a program that reads input until encountering EOF . Have it report the number of uppercase letters and the number of lowercase letters in the input. Assume that the numeric values for the lowercase letters are sequential, and assume the same for uppercase.

  4. Write a program that reads input until encountering EOF . Have it report the average number of letters per word. Don't count whitespace as being letters in a word. Actually, punctuation shouldn't be counted either, but don't worry about that now. (If you do want to worry about it, consider using the ispunct () function from the ctype .h family.)

  5. Modify the guessing program of Listing 8.5 so that it uses a more intelligent guessing strategy. For example, have the program initially guess 50, and have it ask the user whether the guess is high, low, or correct. If, say, the guess is low, have the next guess be halfway between 50 and 100, that is, 75. If that guess is high, let the next guess be halfway between 75 and 50, and so on. Using this binary search strategy, the program quickly zeros in on the correct answer, at least if the user does not cheat.

  6. Modify the getfirst() function of Listing 8.9 so that it returns the first non-whitespace character encountered. Test it in a simple program.

  7. Modify Exercise 7 from Chapter 7 so that the menu choices are labeled by characters instead of by numbers .

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net