Hello! I need to write a program to find whether a word is a palindrome or not. What differentiates this from the other palindrome threads in this forum is that I can only use certain functions. They are: If statements, string functions (to upper, to lower, indoxof, length, substrings), for loop, do while loop, integers, decimals.
My idea is to count from both ends of the string, comparing characters as I go. However, my code doesn't seem to be working past the first and last characters. I would love some helpful feedback or suggestions.
My code so far:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim word, newword, ending As String
        Dim whole, beginning As Integer
        word = txtbox1.Text
        word = word.ToLower
        whole = word.Length - 1
        ending = word.Substring(whole, 1)
        newword = word.Substring(beginning, 1)
        beginning = 0
        If newword = ending Then
            beginning = beginning + 1
            whole = whole - 1
            newword = word.Substring(beginning, 1)
            ending = word.Substring(whole, 1)
txtbox1.Text = "The word is a palindrome."
        Else : txtbox1.Text = "The word is not a palindrome."

Recommended Answers

All 5 Replies

Hi,

You can try this function:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        IsPalindrome(strToCheck:=TextBox1.Text)
    End Sub

 Public Function IsPalindrome(ByVal 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
                MsgBox("It's not a Palindrome")
                TextBox1.Text = ""
                TextBox1.Focus()
            Else
                MsgBox("It's a palindrome")
                TextBox1.Text = ""
                TextBox1.Focus()
            End If
            Exit Do
            iBack = iBack - 1
            iForward = iForward + 1
        Loop

        IsPalindrome = bPalindrome
        Exit Function

ERR_IsPalindrome:

        Debug.Print("Error: " & Err.Description)

    End Function

Thanks for responding, but I can't use the boolean or mid functions. I can only use the functions shown in my original post.

Hi,

You can try this function:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        IsPalindrome(strToCheck:=TextBox1.Text)
    End Sub

 Public Function IsPalindrome(ByVal 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
                MsgBox("It's not a Palindrome")
                TextBox1.Text = ""
                TextBox1.Focus()
            Else
                MsgBox("It's a palindrome")
                TextBox1.Text = ""
                TextBox1.Focus()
            End If
            Exit Do
            iBack = iBack - 1
            iForward = iForward + 1
        Loop

        IsPalindrome = bPalindrome
        Exit Function

ERR_IsPalindrome:

        Debug.Print("Error: " & Err.Description)

    End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim word As String
        word = TxtBox1.Text.ToLower
        For I = 0 To word.Length - 1
            If word.Substring(I, 1) = word.Substring(word.Length - I - 1, 1) Then
                Continue For
            Else
                TxtBox1.Text = "The word is not a palindrome." : Exit Sub
            End If
        Next
        TxtBox1.Text = "The word is a palindrome."
    End Sub

it is actually enough to loop over the half of the word

Sub Main()
	Dim word As String = "lagerregal"
	For I = 0 To word.Length - 1 / 2
		If word.Substring(CInt(I), 1) = word.Substring(word.Length - CInt(I) - 1, 1) Then
			Continue For
		Else
			Console.WriteLine("The word is not a palindrome.")
			GoTo ENDE
		End If
	Next
	Console.WriteLine("The word is a palindrome.")
ENDE:
	Console.Read()
End Sub

Thank you for your help, xcelled194. I managed to solve the problem just before you posted your code. With a few minor changes, your code works the way I need it to, though.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim word As String
        word = TxtBox1.Text.ToLower
        For I = 0 To word.Length - 1
            If word.Substring(I, 1) = word.Substring(word.Length - I - 1, 1) Then
                Continue For
            Else
                TxtBox1.Text = "The word is not a palindrome." : Exit Sub
            End If
        Next
        TxtBox1.Text = "The word is a palindrome."
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.