Read: Read 7.1, 7.2 and 6.6 (about Strings) and 4.4 (about switch and break).
Program:
Define a class MyLine
which has a private string and
methods copyIn
to copy-in and
print
to print the string. Make new methods as follows:
countChar
which takes as argument a
character and returns the number of
times that letter appears in the private string.
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.
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.
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 WORLDNow 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:
isEmpty
which returns 1 if the private string is
empty, 0 else.
index
which takes a string argument s, the
match string, and an
integer argument mode
and scans the private string from
its beginning until:
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.