Word Reverser

The aim of the program is to reverse a word; the program will get a word or phrase from the user then output it the other way round. For example if the user inputted "car" then the program would output "rac".

For this program we can use the 'textwindow' because we are only working with text.
Firstly we need to ask the user for an input, to do this we can use: TextWindow.WriteLine("Enter a word to be reversed...")
To get the input we need to read it from the window and set the value to a variable.
word = TextWindow.Read()
The variable 'word' with now have the value of the input.

Next simply write the following to start the output: TextWindow.WriteLine("The word reversed is:")
Now for the reversing part;

We are going to use a For loop to get each character and output them on the window, so we need to get the characters from the end first and go towards the start.
So we must know the length of the word so we can deduct from it. To get the length of a string we use: wordlength = Text.GetLength(word)

Now we need to start the For loop, it must start from 0 and continue until the length of the word. We can use the variable i as a temporary counter:
For i = 0 To wordlength
EndFor

Now we need to do the part inside the For loop. We need to get a subsection of the phrase; in MS Basic we use: Text.GetSubText(text,start,length)
The text to search for goes before the first comma, in our case 'word'. Then the start position, we must start at the end and go towards the start, so we use: 'wordlength-i'. The length is 1, as we only need one character at a time.
character = Text.GetSubText(word,wordlength-i,1)

Just before the end of the For loop add the following line to output the characters, that will now be in reverse order:
TextWindow.Write(character)

You may have to add a TextWindow.WriteLine("") at the end, otherwise "Press any key to continue.." will make the text difficult to read.

Your completed program should look something like the following:

Finished Program

TextWindow.WriteLine("Enter a word to be reversed...")
word = TextWindow.Read()
TextWindow.WriteLine("The word reversed is:")
wordlength = Text.GetLength(word)
For i = 0 To wordlength  
  character = Text.GetSubText(word,wordlength-i,1)
  TextWindow.Write(character)
EndFor
TextWindow.WriteLine("")




Challenge: Use a while loop to continuously ask for new words until the word entered is exit.






ċ
Word Reverser.zip
(149k)
Jack Joynson,
16 Mar 2013, 08:09