Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Palindrome App

I want to create a palindrome app that determins the word or phrase entered by a user is a palindrome or not. Eg.
--------------
USER TEXT: Able was i ere i saw Elba

labelbox: Able was i ere i saw Elba is a palindrome
-----------
im thinking ill have to have a variable holding the string, then create a new variable holding the 'reverse' of the orignal string, and compare them. But im not sure what string function i should use..Any idea of how to code this?

Thanks.

-I tried the Loop While, but then the For Loop would be better, How would compare the first char to the last? What would the program look like?
[666 byte] By [tygolfer] at [2007-11-11 9:57:04]
# 1 Re: Palindrome App
The loop while was working but then if i did a Loop while: 1 to Len(string), it would just exit the loop and not hold anything that i needed.
tygolfer at 2007-11-11 17:23:32 >
# 2 Re: Palindrome App
Well u need a small for loop read from the left text and put in the right label :

try this :

Dim i As Integer
For i = 1 To Len(TEXT)
labelbox.Caption = Mid(TEXT, i, 1) & labelbox.Caption
Next

also be sure that the label contains nothing before start.
Amahdy at 2007-11-11 17:24:32 >
# 3 Re: Palindrome App
The loop while was working but then if i did a Loop while: 1 to Len(string), it would just exit the loop and not hold anything that i needed.

I think your problem is with using "while" in the place of "until"

ok my posted code will work well isn't ?
Amahdy at 2007-11-11 17:25:30 >
# 4 Re: Palindrome App
a tiny think maybe be better is to convert first letter to upper case using Ucase() function , but there is no rule for knowing witch letter must be converted to lower case !
Amahdy at 2007-11-11 17:26:36 >
# 5 Re: Palindrome App
yes thats true...this is what i have

Private Sub cmdCheck_Click()

Dim i As Integer
Dim strWord As String

lblOutput.Caption = ""
strWord = txtWord.Text

For i = 1 To Len(Text)
lblOutput.Caption = Mid(txtWord.Text, i, 1) & lblOutput.Caption
Next

End Sub

--The label box does not show any thing. so how what what the code look like if the label said:

strWord & "is a palindrome"

Or if its not one i would be:
strWord & "is not a palindrome"

How would the if condition fit and look like?
tygolfer at 2007-11-11 17:27:37 >
# 6 Re: Palindrome App
lblOutput.Caption = ""
strWord = txtWord.Text

For i = 1 To Len(Text)
lblOutput.Caption = Mid(txtWord.Text, i, 1) & labelbox.Caption
Next

what is "TEXT" vb dont know it then it will consider it empty variable and hence the for loop will not be acceed !

try the for loop like this :

For i = 1 To Len(strWord)


so how what what the code look like if the label said:

strWord & "is a palindrome"

Or if its not one i would be:
strWord & "is not a palindrome"

How would the if condition fit and look like?

no if needed u only need a small end after the for loop :

strWord = strWord & " is a palindrome"

remark the space at the begeening of " is a plain..." to have better results .
Amahdy at 2007-11-11 17:28:35 >
# 7 Re: Palindrome App
yes thats true...this is what i have
For i = 1 To Len(Text)
lblOutput.Caption = Mid(txtWord.Text, i, 1) & labelbox.Caption
Next

You may also got compile error coz "labelbox" is not defined u must change it to your label control name ... "lblOutput"

do u have problems understanding the code ?
Amahdy at 2007-11-11 17:29:39 >
# 8 Re: Palindrome App
nope i understand it perfectly...I just forgot to change a couple of words around. My fault. (logic error)

Ok, so now i have:

Private Sub cmdCheck_Click()

Dim i As Integer
Dim strWord As String
Dim strWord2 As String

lblOutput.Caption = ""
strWord = txtWord.Text

For i = 1 To Len(strWord)
lblOutput.Caption = Mid(txtWord.Text, i, 1) & lblOutput.Caption
Next

If strWord = strWord2 Then
lblOutput.Caption = strWord & " is palindrome"
Else
lblOutput.Caption = strWord & " is plain"
End If
End Sub

I need to change this, what do you think?
tygolfer at 2007-11-11 17:30:39 >
# 9 Re: Palindrome App
i edited a few things above.

right now im getting "strWord is Plain" for every word i type in
tygolfer at 2007-11-11 17:31:36 >
# 10 Re: Palindrome App
nope i understand it perfectly...I just forgot to change a couple of words around. My fault. (logic error)

Ok, so now i have:

Private Sub cmdCheck_Click()

Dim i As Integer
Dim strWord As String
Dim strWord2 As String

lblOutput.Caption = ""
strWord = txtWord.Text

For i = 1 To Len(strWord)
lblOutput.Caption = Mid(txtWord.Text, i, 1) & lblOutput.Caption
Next

If strWord = strWord2 Then
lblOutput.Caption = strWord & " is palindrome"
Else
lblOutput.Caption = strWord & " is plain"
End If
End Sub

I need to change this, what do you think?

Not logic errors, typing errors "7" --> "&" :D

okey now what is strword2 ?? you have defined it, so it's empty ;
and finally you compare it with strword witch usually conatins strings, then the first if will never be acceed and u will have plain forever .

better cpmare strword with the original text : "txtWord"

If strWord = txtWord Then

got it ?
Amahdy at 2007-11-11 17:32:36 >
# 11 Re: Palindrome App
lol...7/& :) ..haha

strWord= txtWord wont work because:

if the user inputs: jimmy

strWord = jimmy
txtWord =jimmy

Therefore every word that is inputted says it is a palindrome. :)
tygolfer at 2007-11-11 17:33:37 >
# 12 Re: Palindrome App
I'm feeling u can't understand the code ; I'm talking generally and I'm not making tests for code I brings, I put them to use them when u understand the method ..

anyway no need for variables at all or for the if statment compare with the label ...

If lblOutput.Caption = txtWord Then

this compare between the fianl output and the original in the text box ..

got it this time ?
Amahdy at 2007-11-11 17:34:35 >
# 13 Re: Palindrome App
Omg, i am an idiot, i didn't even think of that..Ahh im sry..Ok the code is done. Thanks alot for your help!
tygolfer at 2007-11-11 17:35:41 >
# 14 Re: Palindrome App
Omg, i am an idiot, i didn't even think of that..Ahh im sry..Ok the code is done. Thanks alot for your help!

No man not an idiot just beginner; sure by time u will know more in programming .. I'm very glade for helping u ,and at any time :)

Best regards.
Amahdy at 2007-11-11 17:36:46 >
# 15 Re: Palindrome App
Amahdy, I dont know any of the case commands you mentioned earlier. The Ucase() im guessing is allowing the first letter to be capatilized.

radar = palindrome
Radar <> palindrome

So in my code where would the Ucase function go? is it like: UCase(strWord,1) ?

CODE:

'checking to see if the word is a palindrome
Private Sub cmdCheck_Click()
'vars
Dim intX As Integer
Dim strWord As String

lblOutput.Caption = "" 'clearing the label
strWord = txtWord.Text 'variable equaling the txtbox

'looping structure:
For intX = 1 To Len(strWord) 'Will check every letter until the end of string 'checks if the letter correspond with the intial input (strWord)
lblOutput.Caption = Mid(txtWord.Text, intX, 1) & lblOutput.Caption
Next

'the word spelled backwards for the USER's proof
lblBackwards.Caption = lblOutput.Caption

'If condition showing the user if palindrome = true/false
If lblOutput.Caption = txtWord Then
lblOutput.Caption = strWord & " is a palindrome"
Else
lblOutput.Caption = strWord & " is plain word"
End If
End Sub
tygolfer at 2007-11-11 17:37:39 >
# 16 Re: Palindrome App
well Ucase() take one string parameter and return this string to upper case letters .

ex :

Dim a As String
a = Ucase("mytest")
'here a = "MYTEST"
MsgBox a
'the inverse function is Lcase()
a = Lcase(a)
'Here a = "mytest" again
MsgBox a
'also the function can take one character :
a = Ucase("t")
MsgBox a 'should be "T"

now you need to remove the first left letter and put it's upper case ;

first make a variable to hold the left letter tmp say ,
second reomve the left letter from the string ,
third reput in the left of the string the Ucase of tmp
ex:

Dim tmp as String
tmp = Left(lblBackwards.Caption, 1)
lblBackwards.Caption = Mid(lblBackwards.Caption, 2)
lblBackwards.Caption = Ucase(tmp) & lblBackwards.Caption

Now with small change u can make it all with one step using function construnction :

lblBackwards.Caption = Ucase(Left(lblBackwards.Caption, 1)) & Mid(lblBackwards.Caption, 2)
Amahdy at 2007-11-11 17:38:40 >
# 17 Re: Palindrome App
Ok so your making the whole word UpperCase, then making the second letter and on Lower Case. That makes sense. Does this replace:

'the word spelled backwards for the USER's proof
lblBackwards.Caption = lblOutput.Caption
lblBackwards.Caption = Ucase(Left(lblBackwards.Caption, 1)) & Mid(lblBackwards.Caption, 2)
tygolfer at 2007-11-11 17:39:47 >
# 18 Re: Palindrome App
Ok so your making the whole word UpperCase, then making the second letter and on Lower Case. That makes sense. Does this replace:

'the word spelled backwards for the USER's proof
lblBackwards.Caption = lblOutput.Caption
lblBackwards.Caption = Ucase(Left(lblBackwards.Caption, 1)) & Mid(lblBackwards.Caption, 2)

Not exactly :
1) Ucase(Left(lblBackwards.Caption, 1)) :
Make the left letter UpperCase

2) Mid(lblBackwards.Caption, 2) :
Is the string starting by the second letter

3) lblBackwards.Caption = Ucase(Left(lblBackwards.Caption, 1)) & Mid(lblBackwards.Caption, 2)
Add them together

Clear ?

Now I think you should get correct results , nop ?

***My brother went to Canada and told me that there ppl talk the french lang. using this palindrom method , changing in some couples I mean .. example : famille >>> mifam, frere >>> erfr ... do u need it for making the computer make that :D
Amahdy at 2007-11-11 17:40:41 >
# 19 Re: Palindrome App
of course it is possible to detect if the string is palindrome comparing only half string, instead of building a new one. this is my solution (that is just another solution, I do not want to say that there is anything wrong with yours!)

Public Function IsPalindrome(ByVal s As String) As Boolean
IsPalindrome = True
Dim k As Long
For k = 1 To Len(s) \ 2
If StrComp(Mid$(s, k, 1), Mid$(s, Len(s) - k + 1, 1), vbTextCompare) <> 0 Then
IsPalindrome = False
Exit For
End If
Next
End Function
mstraf at 2007-11-11 17:41:52 >
# 20 Re: Palindrome App
yea If u r talking about best and fast method ; okey to use it do the following :

put mstraf code in any place in your code [or module]

now in the cmdCheck_Click() event put this code :

if (IsPalindrome(txtWord)) then
lblOutput.Caption = strWord & " is a palindrome"
Else
lblOutput.Caption = strWord & " is plain word"
End If

that will be better and easiest ..
Amahdy at 2007-11-11 17:42:53 >
# 21 Re: Palindrome App
Ok, umm..i kinda want to stick to what i have got so far. Unless you want to code the whole thing for me doing it the 'easy' way? Then i could compare them?

i have the same command buttons and txtboxes/variables
tygolfer at 2007-11-11 17:43:49 >
# 22 Re: Palindrome App
Ok, umm..i kinda want to stick to what i have got so far. Unless you want to code the whole thing for me doing it the 'easy' way? Then i could compare them?

i have the same command buttons and txtboxes/variables

I dont understand what u want exactly here but my last post is talking about that :

in the cmd_click() event only put my last code and put mstraf function in any place or module ,, you will have good results , but also I think before mstraf post u got a good working code , not ?

just a bit , if u don't know yet more about functions it's similarly to that :

Private Sub cmdCheck_Click()
Dim NotPalindrome As Boolean
Dim k As Integer
For k = 1 To Len(txtWord) \ 2
If StrComp(Mid$(txtWord, k, 1), Mid$(txtWord, Len(txtWord) - k + 1, 1), vbTextCompare) <> 0 Then
NotPalindrome = True
Exit For
End If
Next

if (NotPalindrome) then
lblOutput.Caption = txtWord & " is plain word"
Else
lblOutput.Caption = txtWord & " is a palindrome"
End If
End Sub

use the above code if you need final results only ..
Amahdy at 2007-11-11 17:44:47 >
# 23 Re: Palindrome App
Yes the code i had before works great. This code u shown me above also works good. Thnx.
tygolfer at 2007-11-11 17:45:55 >
# 24 Re: Palindrome App
what does the strComp and vbTextCompare functions do?
tygolfer at 2007-11-11 17:46:54 >
# 25 Re: Palindrome App
what does the strComp and vbTextCompare functions do?

search in the MSDN for "strComp" u will find good explaination .

**It exist directly in the index search ..
Amahdy at 2007-11-11 17:47:48 >