Homework Assignment 7

Assigned: Monday,March 18.
Due: Wednesday, March 27.

Read: Read 7.1, 7.2 and 6.6 (about Strings) and 4.4 (about switch and break).

Program:
Define a class MyLinewhich has a private string and methods copyIn to copy-in and print to print the string. Make new methods as follows:

  1. A method countChar which takes as argument a character and returns the number of times that letter appears in the private string.
  2. A method countChars which takes as argument a string and returns the number of times any letter in that string, called the match string, appears in the private string.
  3. A method translate which takes two strings as arguments, a match string and a translation string, and replaces each occurrence of a character in the match string by the corresponding character in the translation string. The caller must make sure that the match and translation strings are the same length, and a corresponding character between the match and translation strings are characters which occur in the same position in each string.
Example:
       MyLine line( "Hello World" ) ;
       cout << line.countChar( 'o' ) << endl ;
       cout << line.countChars( "aeiou " ) << endl ;
       line.translate("abcdefghijklmnopqrstuvwxyz",
                      "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ;
       line.print() ;
  
When run, you get:
      2
      4
      HELLO WORLD
  
Now write a program which accepts a line of input and outputs the line, each word in the input on a separate line. Use the class you defined, and the following methods. Write and test each method one by one before using them in the correct sequence to complete the assignment:
  1. A method isEmpty which returns 1 if the private string is empty, 0 else.
  2. A method index which takes a string argument s, the match string, and an integer argument mode and scans the private string from its beginning until:
    1. if mode=1, a character in the match string is found,
    2. if mode=-1, a character NOT in the match string is found.
    The method returns this index. If no match is found, the length of the string is returned, that is, the index of the NULL character.
  3. A method prefix which takes a string argument and an integer i and copies into the string argument the first i characters in the private string. It adds a null character to the end of the copy, and it then removes the first i characters from the private string. That is, the private string now starts at the i-th character.