Exercises

  1. 21.4 (Fill in the Blanks) Fill in the blanks in each of the following:

    1. Class string member function       converts a string to a pointer-based string.

    2. Class string member function       is used for assignment.

    3.       is the return type of function rbegin.

    4. Class string member function       is used to retrieve a substring.

  2. 21.5 (True or False) State which of the following statements are true and which are false. If a statement is false, explain why.

    1. strings are always null terminated.

    2. Class string member function max_size returns the maximum size for a string.

    3. Class string member function at can throw an out_of_range exception.

    4. Class string member function begin returns an iterator.

  3. 21.6 (Find Code Errors) Find any errors in the following and explain how to correct them:

    1. std::cout << s.data() << std::endl; // s is "hello"

    2. erase(s.rfind("x"), 1); // s is "xenon"

    3.  

      
      string& foo() {
          string s("Hello");
          ...   // other statements
          return;
      }
      
  4. 21.7 (Simple Encryption) Some information on the Internet may be encrypted with a simple algorithm known as “rot13,” which rotates each character by 13 positions in the alphabet. Thus, 'a' corresponds to 'n', and 'x' corresponds to 'k'. rot13 is an example of symmetric key encryption. With symmetric key encryption, both the encrypter and decrypter use the same key.

    1. Write a program that encrypts a message using rot13.

    2. Write a program that decrypts the scrambled message using 13 as the key.

    3. After writing the programs of part (a) and part (b), briefly answer the following question: If you did not know the key for part (b), how difficult do you think it would be to break the code? What if you had access to substantial computing power (e.g., supercomputers)? In Exercise 21.24 we ask you to write a program to accomplish this.

  5. 21.8 (Using string Iterators) Write a program using iterators that demonstrates the use of functions rbegin and rend.

  6. 21.9 (Words Ending in “r” or “ay”) Write a program that reads in several strings and prints only those ending in “r” or “ay”. Only lowercase letters should be considered.

  7. 21.10 (string Concatenation) Write a program that separately inputs a first name and a last name and concatenates the two into a new string. Show two techniques for accomplishing this task.

  8. 21.11 (Hangman Game) Write a program that plays the game of Hangman. The program should pick a word (which is either coded directly into the program or read from a text file) and display the following:

    
    Guess the word:    XXXXXX
    

    Each X represents a letter. The user tries to guess the letters in the word. The appropriate response yes or no should be displayed after each guess. After each incorrect guess, display the diagram with another body part filled. After seven incorrect guesses, the user should be hanged. The display should look as follows:

    
      O
     /|
      |
     / 
    

    After each guess, display all user guesses. If the user guesses the word correctly, display

    
    Congratulations!!! You guessed my word. Play again? yes/no
    
  9. 21.12 (Printing a string Backward) Write a program that inputs a string and prints the string backward. Convert all uppercase characters to lowercase and all lowercase characters to uppercase.

  10. 21.13 (Alphabetizing Animal Names) Write a program that uses the comparison capabilities introduced in this chapter to alphabetize a series of animal names. Only uppercase letters should be used for the comparisons.

  11. 21.14 (Cryptograms) Write a program that creates a cryptogram out of a string. A cryptogram is a message or word in which each letter is replaced with another letter. For example the string

    
    The bird was named squawk
    

    might be scrambled to form

    
    cin vrjs otz ethns zxqtop
    

    Spaces are not scrambled. In this particular case, 'T' was replaced with 'x', each 'a' was replaced with 'h', etc. Uppercase letters become lowercase letters in the cryptogram. Use techniques similar to those in Exercise 21.7.

  12. 21.15 (Solving Cryptograms) Modify Exercise 21.14 to allow the user to solve the cryptogram. The user should input two characters at a time: The first character specifies a letter in the cryptogram, and the second letter specifies the replacement letter. If the replacement letter is correct, replace the letter in the cryptogram with the replacement letter in uppercase.

  13. 21.16 (Counting Palindromes) Write a program that inputs a sentence and counts the number of palindromes in it. A palindrome is a word that reads the same backward and forward. For example, "tree" is not a palindrome, but "noon" is.

  14. 21.17 (Counting Vowels) Write a program that counts the total number of vowels in a sentence. Output the frequency of each vowel.

  15. 21.18 (String Insertion) Write a program that inserts the characters "******" in the exact middle of a string.

  16. 21.19 (Erasing Characters from a string) Write a program that erases the sequences "by" and "BY" from a string.

  17. 21.20 (Reversing a string with Iterators) Write a program that inputs a line of text and prints the text backward. Use iterators in your solution.

  18. 21.21 (Recurively Reversing a string with Iterators) Write a recursive version of Exercise 21.20.

  19. 21.22 (Using the erase Functions with Iterator Arguments) Write a program that demonstrates the use of the erase functions that take iterator arguments.

  20. 21.23 (Letter Pyramid) Write a program that generates the following from the string "abcdefghijklmnopqrstuvwxyz":

    
                 a
                bcb
               cdedc
              defgfed
             efghihgfe
            fghijkjihgf
           ghijklmlkjihg
          hijklmnonmlkjih
         ijklmnopqponmlkji
        jklmnopqrsrqponmlkj
       klmnopqrstutsrqponmlk
      lmnopqrstuvwvutsrqponml
     mnopqrstuvwxyxwvutsrqponm
    nopqrstuvwxyz{zyxwvutsrqpon
    
  21. 21.24 (Simple Decryption) In Exercise 21.7, we asked you to write a simple encryption algorithm. Write a program that will attempt to decrypt a “rot13” message using simple frequency substitution. (Assume that you do not know the key.) The most frequent letters in the encrypted phrase should be replaced with the most commonly used English letters (a, e, i, o, u, s, t, r, etc.). Write the possibilities to a file. What made the code breaking easy? How can the encryption mechanism be improved?

  22. 21.25 (Enhanced Employee Class) Modify class Employee in Figs. 12.912.10 by adding a private utility function called isValidSocialSecurityNumber. This member function should validate the format of a social security number (e.g., ###-##-####, where # is a digit). If the format is valid, return true; otherwise return false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset