|
|
|
|
|
|
|
Input: A series of names, with one person's name (first and last) on each line of input in the form |
|
|
|
|
|
|
|
|
where there may be any number of blanks preceding the first name and one or more blanks between the two names. |
|
|
|
|
|
|
|
|
The number of input lines is unknown. The program should continue to process input lines until EOF occurs. |
|
|
|
|
|
|
|
|
Output: A corresponding series of lines of the form |
|
|
|
|
|
|
|
|
Discussion: This task is an easy one to do by hand. We would read the two names; write down the last, followed by a comma; and write down the first letter of the first name, followed by a period. Basically, this is how we program the problem. The hard part is trying to simulate reading the two names. The program has to read one character at a time, examine it, and decide what to do with it. |
|
|
|
|
|
|
|
|
Let's analyze this process by hand, going character-by-character through the input. The first character is either a blank or a letter. If it is a letter, we need to save it because it is the first initial. |
|
|
|
|
|
|
|
|
Once we have the first initial, we are not interested in the rest of the first name. So we must continue to read and ignore characters until we reach the last name. How do we recognize the beginning of the last name? It is the first letter after the blanks following the first name. Once we find the last name, we continue reading and printing each character until we reach the end of the input line. Then we print a comma followed by a blank and the first initial, which we saved, followed by a period. |
|
|
|
|
|
|
|
|
Now that we have analyzed the problem, we can do our top-down design. |
|
|
|
|
|
|
|
|
Assumptions: Middle names are not present in the input. The end of an input line comes immediately after the person's last name. |
|
|
|
|
|
|
|
|
Get initial
WHILE NOT EOF
Print last name
Print initial
Get initial |
|
|
|
|
|
|