This slide show will cover character manipulation to flip a name that is in the format last/first mid to first mid last. In this course we will flip the name using individual character access and using functions.
First we will discuss doing this through character by character manipulation. In another slide we will discuss using functions.
For example, in COBOL you could define the name and then redefine it as a table. The thing that occurs would be a single character and it the occurs would contain the number of characters in the field.
In COBOL there is an Inspect verb that allows you to locate a particular character in a string. Other languages have similiar commands.
In most languages there is some kind of length function that will return this information.
This process will vary depending on the language you choose. To do this example, you must choose a lanaguage that allows individual access to characters.
The next example will be done with functions, to do this example you must choose a language that has the functions to support this manipulation.
This shows the information that is needed to initialize before the loop to move the characters of the first name plus middle name (if there) to the output area. Note that this loop will terminate when the character in the LASTLOC has been moved. The control of the loop is therefore a comparison between the value of PTIN and the value in LASTLOC.
SLSHLOC is the location of the slash
LASTLOC is the location of the last character
PTIN is the pointer pointing to the character to be moved (it is initialized to point to the first character of the first name)
PTOUT is the pointer pointing to the character to move to in the output area (it is initialized to point to the first character of the output area)
NCH is the name of the individual character in the input area (the name to be flipped)
FCH is the name of the individual character in the output or flipped area (the name after it is flipped)
Note that the moving or assigning of the input character to the output character and the subsequent incrementing of the subscript/index/pointer should be done in a loop.
The loop should end when the last character of the first + middle name has been moved. This is why we need LASTLOC.
Moving the second character of the first name.
You are now looping moving the next character in the input to the next character in the output. The loop will terminate when PTIN indicates that the last character of the first + middle has been moved.
The loop processing is simply to move the character, increment the index/subscript/pointer.
Whether the loop continues or not is dependent on whether the last character has been moved. LASTLOC is the field to compare against to make that determination.
I have now moved the entire first name.
This moved the space between Alice and Mary. To preserve the integrity of the name, the space must be moved.
This is now moving the first character of the middle name which is the fifteenth character to the seventh slot on the output.
Another move - the loop continues because the last character has not been moved.
Another iteration of the loop.
At this point, when PTIN is incremented by 1 it equals 19 and 19 is greater than LASTLOC which is 18 so the loop ends. I am assuming that the testing of whether to do another iteration of the loop comes after PTIN and PTOUT have been incremented so I am doing a greater than comparison. If the test is done on the data prior to incrementing, then you would be testing for equality between PTIN and LASTLOC.
The next step is to prepare for the next loop by resetting the index/subscript/pointers.
You now want to point to the first character of the input which contains the first character of the first name so you reset PTIN to 1.
You now want to point to skip a space and point to the next character of the output so you increment PTOUT by 1. (Note if you had not already added to PTOUT, you would increment by 2)
You are now in a loop that will move the last name to the output one character at a time. This loop will terminate when you have moved the last character of the last name. It can be checked by comparing to the location of the slash since the slash is immediately after the last character of the last name. Remember, the location of the slash is in SLSHLOC and for this particular example it is 8.
The second character of the last name is moved.
The third character of the last name is moved.
The fourth character of the last name is moved.
The fifth character of the last name is moved. Remember this loop continues until the last name has been entirely moved. SLSHLOC is the comparison point to determine that it is complete.
The sixth character of the last name is moved.
At this point PTIN is equal to SLSHLOC so the loop will terminate. The name has been flipped.
Working with functions and commands. Check your language of choice for functions and commands that provide these abilities.