|
-
Oct 11th, 2000, 09:15 PM
#1
Thread Starter
New Member
I would like to know what code i would need , or an example of code for the following.
i would like to enter say ten names in a text file,
then i want the program to check if a word is a Palindrome or not.
any idea's how to use instr to do this ... your help would be appreciated
ps : a Palindrome is a word that is spelt backwards and forwards the same , eg : WayaW
-
Oct 11th, 2000, 09:53 PM
#2
Hyperactive Member
small bit of help
Here you are. It just looks at the contents of a text box to see if it is a palindrome or not.
You should be able to adapt it easily to your needs.
Code:
Private Sub Command1_Click()
Dim myText As String
Dim c As Integer
myText = Text1.Text
If Len(myText) Mod 2 <> 1 Then
MsgBox myText & " is not a Palindrome"
Else
For c = 1 To Len(myText) / 2
If Mid(myText, c, 1) <> Mid(myText, Len(myText) - c + 1, 1) Then
MsgBox myText & " is not a Palindrome"
Exit Sub
End If
Next
MsgBox myText & " is a Palindrome"
End If
End Sub
-
Oct 11th, 2000, 10:15 PM
#3
Sheesh! Paul beat me to it! But since I worked so hard on it, I will post my answer anyway. Slap a command button and a listbox on your form, then copy and paste the following code for your form:
Code:
Option Explicit
Private Sub Command1_Click()
Dim arrWords(1 To 5) As String
Dim intX As Integer
arrWords(1) = "radar"
arrWords(2) = "Naomi, did I moan?"
arrWords(3) = "Visual Basic"
arrWords(4) = "comp-u-geek"
arrWords(5) = "Madam, I'm Adam"
List1.Clear
For intX = 1 To 5
If IsPalindrome(arrWords(intX)) Then
List1.AddItem Chr$(34) & arrWords(intX) & Chr$(34) _
& " is a palindrome."
Else
List1.AddItem Chr$(34) & arrWords(intX) & Chr$(34) _
& " is not a palindrome."
End If
Next
End Sub
Public Function IsPalindrome(strWord As String) As Boolean
Dim strStrippedWord As String
Dim strTestChar As String * 1
Dim intPosLeft As Integer
Dim intPosRight As Integer
Dim intLen As Integer
Dim intX As Integer
Const conAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
' remove all non-letters from the original word
intLen = Len(strWord)
strStrippedWord = ""
For intX = 1 To intLen
strTestChar = Mid$(strWord, intX, 1)
If InStr(conAlphabet, strTestChar) > 0 Then
strStrippedWord = strStrippedWord & strTestChar
End If
Next
'check to see if its a palindrome
IsPalindrome = True 'innocent until proven guilty
intPosLeft = 1
intPosRight = Len(strStrippedWord)
' convert all chars to uppercase
strStrippedWord = UCase$(strStrippedWord)
Do Until intPosLeft >= intPosRight
If Mid$(strStrippedWord, intPosLeft, 1) _
= Mid$(strStrippedWord, intPosRight, 1) Then
intPosLeft = intPosLeft + 1
intPosRight = intPosRight - 1
Else
IsPalindrome = False
Exit Do
End If
Loop
End Function
"It's cold gin time again ..."
Check out my website here.
-
Oct 12th, 2000, 03:55 AM
#4
Addicted Member
PaulLewis:
Sorry to complain on you, but your method doesn't work on wards like 'anna' or 'tinnit' (OK, it's not a real word, not even in Swedish, but still...)
Cannot a palindrome have an even number of digits?
I shall see if I can find a way round it.
Cheers,
Pentax
Wilhelm Tunemyr,
Swede in London
[email protected]
"Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
Heinrich Heine (1797-1856)
Pravda vítezi!
(Truth prevails!)
-
Oct 12th, 2000, 04:14 AM
#5
Hyperactive Member
True
I did not realise that it was allowed to have an even number of letters.
So just take away the first test and you'll be fine. Sorry for my confusion 
Code:
Private Sub Command1_Click()
Dim myText As String
Dim c As Integer
myText = Text1.Text
For c = 1 To Len(myText) / 2
If Mid(myText, c, 1) <> Mid(myText, Len(myText) - c + 1, 1) Then
MsgBox myText & " is not a Palindrome"
Exit Sub
End If
Next
MsgBox myText & " is a Palindrome"
End Sub
Cheers
-
Oct 12th, 2000, 05:20 AM
#6
Frenzied Member
BruceG
That's a pretty neat solution!
I like this bit:
If InStr(conAlphabet, strTestChar) > 0 Then
VERY neat!
I usually do something using Asc(strTestChar)
-
Oct 12th, 2000, 08:08 AM
#7
For VB 6 try this code:
Note: It is NOT case-sensitive. You could remove the UCASE statements to make it case-sensitive.
Code:
Public Function IsPalinDrome(ByRef S As String)
IsPalinDrome = UCase(S) = UCase(StrReverse(S))
End Function
To Test the code try this:
Code:
Private Sub Form_Load()
Debug.Print IsPalinDrome("Test")
Debug.Print IsPalinDrome("A name")
Debug.Print IsPalinDrome("Wayaw")
Debug.Print IsPalinDrome("Madam")
Debug.Print IsPalinDrome("Radar")
End Sub
Enjoy!
-
Oct 12th, 2000, 08:37 AM
#8
Frenzied Member
for a phrase to be a Palindrome punctuation is ignored.
RobIII'a and PaulLewis's solutions are therefore incomplete whereas BruceG's is spot on!
[Edited by Mark Sreeves on 10-12-2000 at 10:34 AM]
-
Oct 12th, 2000, 09:44 AM
#9
i want the program to check if a word is a Palindrome or not.
My code is just the shortest and easiest way to check for a palindrome word as you can see. I never said it was for complete phrases. But I must admit you're right a teeny weeny little bit... 
[Edited by RobIII on 10-12-2000 at 10:46 AM]
-
Oct 12th, 2000, 01:23 PM
#10
Hyperactive Member
Don't underestimate the value...
Mark Sreeves:
Don't underestimate the value of delivering solutions that
do what the customer asks for. It may be nice to go the
extra mile and make it do even more, but in the real world,
customers seldom enjoy paying for software that does more
than they originally request.
This is not a defensive statement regarding your post at
all. Just a comment to any aspiring programmers who plan
one day to program for a living.
For most of the short term contracts I've been involved in,
the key requirements were: on-time delivery, delivery of
requirements, ease of use, reliability, documentation and
testing. Never have I had a disappointed customer due to
failure to deliver more than what was asked.
Having said that, I must agree that for any word including
punctuation (apostrophe or hyphen for example), the code I
did which seemed to me to be the simplest way of checking,
would not be complete.
To change the code, I would simply replace all characters
in the string that were not a-z or A-Z with "". Then do
the same test as I already do.
Regards
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|