Saturday, June 3, 2017

Palindrome in Visual Basic 6

In this article I would like to share with you a simple program that will ask the user to give a string and then our program will check if the given string is a palindrome or not a palindrome.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing

Private Sub Command1_Click()

display1 = "The given string " & Trim(UCase(Text1.Text)) & " is a Palindrome."
display2 = "The given string " & Trim(UCase(Text1.Text)) & " is Not a Palindrome."

If IsPalindromeStrict(Text1.Text) = True Then
    MsgBox (display1)
  Else
     MsgBox (display2)
End If

End Sub


Public Function IsPalindromeStrict(pstrText As String) As Boolean
    Dim i As Long
    Dim iMin As Long
    Dim iMax As Long
    
    iMin = 1
    iMax = Len(pstrText)
    For i = 0 To (iMax - iMin) \ 2
        If Mid$(pstrText, iMin + i, 1) <> Mid$(pstrText, iMax - i, 1) Then Exit Function
    Next
    IsPalindromeStrict = True
End Function
 

Public Function IsPalindrome(ByVal pstrText As String) As Boolean
    Dim i As Long
    Dim iMin As Long
    Dim iMax As Long
    
    pstrText = LCase$(pstrText)
    
    i = 1
    Do
        Select Case Mid$(pstrText, i, 1)
            Case "a" To "z", "0" To "9": i = i + 1
            Case Else: pstrText = Replace(pstrText, Mid$(pstrText, i, 1), vbNullString)
        End Select
    Loop While i <= Len(pstrText)
    
    iMin = 1
    iMax = Len(pstrText)
    For i = 0 To (iMax - iMin) \ 2
        If Mid$(pstrText, iMin + i, 1) <> Mid$(pstrText, iMax - i, 1) Then Exit Function
    Next
    IsPalindrome = True
End Function

Private Sub Command2_Click()
End
End Sub


No comments:

Post a Comment