NAME FLIP:
For this problem assume that you have a name that comes in as last name/first name and then a middle name or middle initial if there is one:
Examples:
Johnson/Alice Mary
Daniels/Stephen
Ames/Jennifer A
Hersey/Carl M
Madison/John Lee Andrews
The goal is to flip the name so it will display or print as:
Alice Mary Johnson
Stephen Daniels
Jennifer A Ames
Carl M Hersey
John Lee Andrews Madison
Looking at what you need to do this:
- You need to be able to access characters individually so you can manipulate them
- You need to know the location of the /
- You need to know where the first name + middle name or initials end
Notes:
- To be able to access each character individually, you need to define the field as a table or array with PIC X and OCCURS the length of the field. This should be done for both the input field that is being read and the output area that will hold the results of the flip. You will be moving the data to the output area one character at a time. Clearly, you also need two subscripts - one to control where you are in the input field and one to control where you are in the output field.
- To find the location of the slash, you can use the INSPECT statement which will examine a field in many different ways. For this example you need to use the INSPECT to TALLY the number of characters before the /.
- Since you don't know how many embedded spaces there are in the first + middle names, you would have to search for two spaces together. However this might present problems if the name filled the whole field or there was only one space at the end of the field. The coding is possible, but complicated. A cleaner way to handle it is to start from the back and check each character until you find a non-blank. The first non-blank is the end of the first or first + middle name.
The processing would be:
- Find the location of the slash and the location of the end of the name
- Position the input subscript so it points at the first character for the first + middle name (this would be the character after the slash) and position the output subscript so it points to the first character of the output area.
- Move the first + middle, one character at a time, until you move the last character of the first + middle (remember, this is one of the locations you found)
- Reset the input subscript so it points to the first character of the input field and position the output subscript so it will leave a space between the first + middle and the last name that you are about to move.
- Move the last name, one character at a time until you encounter the slash in the input