-
VB Help Again, Please
Okay.. I'm stuck on the whole Palindromes bit.. Which as a side note, I thought it was interesting that Aibohphobia is the fear of Palindromes.. I'm sure you've all done the ol' Palindrome program.. Well.. Yeah.. I'm stuck.. But.. I was wondering if there was a website that I could just look up functions needed or just straight up code for frequently used projects.. Such as the palindrome one.. Anyone know of anything? Thanks..
-
this should do it
Public Function IsPalindrome(strToCheck As String)
As Boolean
Dim iForward As Integer
Dim iBack As Integer
Dim iMid As Integer
Dim bPalindrome As Boolean
IsPalindrome = False
bPalindrome = True
On Error GoTo ERR_IsPalindrome
iBack = Len(strToCheck)
iMid = iBack / 2
iForward = 1
If (iBack < 1) Then
Exit Function
End If
Do While (iForward <> iBack And
iForward <= iMid)
If (Mid(strToCheck, iForward, 1)
<> Mid(strToCheck, iBack, 1))
Then
bPalindrome = False
Exit Do
End If
iBack = iBack - 1
iForward = iForward + 1
Loop
IsPalindrome = bPalindrome
Exit Function
ERR_IsPalindrome:
Debug.Print "Error: " & Err.Description
End Function
-
Thanks
Thanks for the help.. But I think that is slightly to advanced for what I'm looking for.. I think I may have just ****ed up my code completely.. But here's what I have:
Code:
Private Sub cmdExe_Click()
Dim P1 As String, NS As String, P2 As String, P3 As String
'P1 = Palindrome #1
'NS = Second half of Palindrome #2 (New String)
'P2 = First half of Palindrome #1
'P3 = New string using strictly characters from 65 thru 91
Dim x As Integer, h As Integer
'x will contain the length of the palindrome
'h will be half the length of x
Open "a:/Palindromes.txt" For Input As #1
Do While Not EOF(1)
Input #1, P1
P1 = UCase(P1)
x = Len(P1)
h = 0
Do While h < x
If (Chr(Mid(P1, x, 1)) >= (Chr(65))) And (Chr(Mid(P1, x, 1)) <= (Chr(91))) Then
P3 = P3 + Chr(Mid(P1, x, 1))
End If
Loop
h = x / 2
If x Mod 2 = 1 Then
h = h + 1
End If
Do While x > h
NS = NS + Mid(P1, x, 1)
x = x - 1
Loop
If x Mod 2 = 1 Then
h = h - 2
End If
P2 = Left(P1, h)
If NS = P2 Then
Print P1; Tab(20); "IS A PALINDROME"
Else
Print P1; Tab(20); "IS NOT A PALINDROME"
End If
Loop
Close #1
End Sub
Thanks for the help..
-
and how about this one?
I made it a littl ebit quck so there are maybe some mistakes... but do you get that?
Code:
Private Sub Command1_Click()
Dim P1 As String, P3 As String
'P1 = Palindrome #1
'P3 = Reversed string
Dim x As Integer,
'x will contain the length of the palindrome
Open "e:/Palindrom.txt" For Input As #1
Do While Not EOF(1)
Input #1, P1
P3 = "" ' empty this variable
P1 = UCase(P1) 'part of your code:)
x = Len(P1) 'your code:)
Do While x > 0 'this cycle revers the word
P3 = P3 & Mid$(P1, x, 1)
x = x - 1
Loop
If P3 = P1 Then
MsgBox P1 & " IS A PALINDROME"
Else
MsgBox P1 & " IS NOT A PALINDROME"
End If
Loop
Close #1
End Sub
-
You could also read the whole line from the file with Line Input, if you have some palindrome sentences. Or was lineinput without the space... Damn with these computers without VB... Then you must make sure you filter spaces and commas and others away.
A minor thing, it should be Open "a:\Palindromes.txt", notice backslash, not slash...
And I would be using a for-loop, like this:
Code:
For I = len(Pl) to 1 step -1
'stuff-done-with-the-MID-function
Next I
But I won't be making a prog now since these were my main points for now and sun7dots already did a quite nice prog. :)
-ZeroOne :cool:
-
Hmm.. I'm going to also need to check on numbers and even sentences.. That's also where I am lost.. Also.. Even/Odd number of letters matters, too..
If you are helping and need to test the text file.. Try these:
NOON
12321
Madam I'm Adam
12345
123321
RACECAR
That should give all the possibilities..
Right now.. My code is changed.. But I need to figure out how to do these ^
Thanks for the help.
-
We had this thread about determining if a number is odd or even: http://www.antionline.com/showthread...hreadid=220143
Also check this thread, which lists a lot of good Visual Basic resources: http://www.antionline.com/showthread...hreadid=222101
-
So how about this.. It's almost the same as the previous one, but the special characters as ' etc are not counted as characters. I'm not sure if I got your point what else you want to do but this program worked on your test file... (I suppose that Madam I'm Adam shoul be palindrom) If I get something wrong please let me now:)
And here goes code #2...
Code:
Private Sub Command1_Click()
Dim P1 As String, P3 As String
Dim P2 As String
Dim OneCharCode As Byte
'P1 = Palindrome #1
'P3 = Reversed string
Dim x As Integer
'x will contain the length of the palindrome
Open "e:/Palindrom.txt" For Input As #1
Do While Not EOF(1)
Input #1, P1
P3 = ""
P2 = "" ' empty this variable
P1 = UCase(P1) 'part of your code
x = Len(P1) 'your code
Do While x > 0 'this cycle revers the word
If (Asc(Mid$(P1, x, 1)) > 47) And (Asc(Mid$(P1, x, 1)) < 123) Then
P3 = P3 & Mid$(P1, x, 1)
End If
x = x - 1
Loop
For x = 1 To Len(P1)
If (Asc(Mid$(P1, x, 1)) > 47) And (Asc(Mid$(P1, x, 1)) < 123) Then
P2 = P2 & Mid$(P1, x, 1)
End If
Next x
If P3 = P2 Then
MsgBox P1 & " IS A PALINDROME"
Else
MsgBox P1 & " IS NOT A PALINDROME"
End If
Loop
Close #1
End Sub
Hope this helps...
btw I'm really not sure if I have right the numbers in asc function but visual basic help doesn't work me right now but it should be ascii code of first char and ascii code of last char...
-
Actually, capital A is asc(65), capital Z asc(90), small a asc(97) and small z asc(122). There are some special characters like ^ (94) and _ (95) between them. So you need just a little bit more complicated formula, like this:
Code:
If (Asc(Mid$(P1, x, 1)) >= 65) And (Asc(Mid$(P1, x, 1)) <= 90) Or (Asc(Mid$(P1, x, 1)) >= 97) And (Asc(Mid$(P1, x, 1)) <= 122)
-
thanx for correcting me ZeroOne:) I have forgoten on that special characters... So thanx...