
Chapter 6
Part B

To solve a problem on the
computer, first think about how you would solve the problem without
the computer. To see if a word is a palindrome, form a new word by reversing
the letters in the word and compare the new phase with the original
one. If they are the same, the word is a palindrome, otherwise it is
not! For example, take the word (let's ignore the case of the letters
for now).
Anna
Form a new word by taking
the last letter and make it the first letter of a new word
a
Take the next letter from
the end letter and add it to the word
na
Take the next letter from
the end letter and add it to the word
nna
Take the next letter from
the end letter and add it to the word
Anna
The resulting word "Anna"
is the same as the original, so Anna is a palindrome!

You should recognize the fact that we are doing the same process over
and over. In computer programming, we recognize this as a loop. So we
could restate the process above as a loop. We should recognize that
it is a finite loop because we know how many times to repeat the process.
How do we know? We can simply count the number of letter is the word.
Thus
For each letter in the word beginning with the last moving to the first
add the last letter of
the original word to the new word
Now we can say
If (the original word is
the same as the new word) then
the original word is a
palindrome
else
it is not

So how do we "pick out" each letter of the word? Recall the mid( )
function:
Mid(string, start,
length)
Well, we know what string we want to inspect (the original word) and
we know how many letters we want to pick out each time (we are taking
one letter at a time. So, assuming that the original word was typed
into txtInput.Text, we know our function will look something
like this
Mid(txtInput.Text,
start, 1)
For start, we want to start at the last letter and move backwards to
the first. Consider,
For each letter from the
last to the first (one at a time)
NewWord = NewWord +
Mid(txtInput.Text, the letter we are looking at, 1)
OK, since we are using a
finite loop, we know we will use a For loop. To say "For each
letter of the word from the last to the first" use
For Start = Len(txtInput.Text)
to 1 Step -1
The new word is built by
using
strNewWord
= strNewWord + Mid(txtInput.Text, Start, 1) 'Remember Start is a variable!
Now we can use an If..Then
to see the word is a palindrome
If (strNewWord = txtInput.Text)
Then
it is a palindrome
else
it is not
|