Non-COBOL Notes
If you were really doing this, you would use functions. However this is an exercise in manipulating data and understanding how it works so I have said no functions for the exercise. COBOL makes this reasonably painless - in other languages it will take more effort!
To do the name flip in VB character by character without using functions, you have to put the data into the array. To the best of my knowledge, there is not a way to take an existing field and describe it as an array the way there is in COBOL. I believe that the same holds true in C++ and Oracle. If someone comes up with a way to handle it differently, I would love to see the solution.
Anyway, what I did in VB was set up an array where I put the text, character by character, that came in and then another array where I put the text, character by character, as I flipped it. Each of these arrays is going to hold 25 elements and for me the element is one character. An example of the array is:
Dim arrayCN(25) As String
Then when the user entered the name and clicked on the button to flip the name, I did a subroutine I called Make Array. In this subroutine I did use functions to get the data in the array - I am only holding you to the one character at a time without functions to do the actual flip. The steps I took were:
- I set up two integer counters - one to point to the array where I was putting the text and one to be used in conjunction with the Mid function to access the name in the next box. I initialized both counters at 1.
- I had an integer to hold the length of the name and I used the name function to get the length.
- I set up a Do While that moved the characters from the name in the text box to the one character elements in the array. I used the results of the length function to determine when I was done.
- I then discovered that I wanted to make sure there was spaces in the rest of the elements in the array so I did a Do Until loop to move a space to all of the unused elements in the array.
Now I had the data in the array and I was ready to flip it.
- The first thing that I did was a subroutine to find the slash. I did this using a DO While and checking each element in the array (from the beginning) until the slash was found.
- I know that the character after the slash is the beginning of the first name.
- Since I said no functions, I cannot use length to find the length of the name so I know where the first name plus any middle names or initials ends, so I use a subroutine that comes in from the back until it finds the first non space. This is why I filled the unused characters of the array with spaces when I was setting it up!
- I then initialized counters to start moving. The array that contains the data will be controlled by a counter that starts with the character after the slash and the array where I am putting the flipped name uses a counter that starts with one. I then move the characters one at a time until I encounter the end of the data as determined above.
- I then reset the counters so the counter controlling the original name is set to one to start moving the last name and the counter controlling where I am putting the data is incremented to leave a space between the first middle combination and the last. I then move the last name one character at a time until the slash is encountered (I can either check for the actual slash or I can use my knowledge of which character element in the array contains the slash).
- To display the name I go through a loop concatenating each character in the output array onto the text I plan to display. When the loop is done, I can display the flipped name.