locked
visual basic 2010 palindrome assignment .pls help RRS feed

  • Question

  • hi will you pls help me in my code. why i cant get the right output for a palindrome sentence. any help is appreciated. thank you.

     

    Public Class Form1
        'declaring variables
        Dim strword As String
        Dim intindex As Integer
        Dim strarr() As String





        Private Sub btnnext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnnext.Click
          
            If Not isgoodinput(strword) Then 'closes the program if its a bad input
                Exit Sub
            End If

            ReDim Preserve strarr(intindex)

            strarr(intindex) = strword
            intindex += 1

            txtsentence.Text += strword


            txtword.Clear()
            txtword.Focus()

        

        End Sub



        Private Function isgoodinput(ByRef word As String) As Boolean
            'weeding out bad input
            If IsNumeric(txtword.Text) Or (txtword.Text) = "" Then
                MsgBox("Please enter a word.", , "Input Error")
                txtword.Focus()
                Return False
            Else
                word = txtword.Text
                Return True

            End If
        End Function


        Private Function ispal(ByVal strarr() As String) As Boolean
            Dim intupper = strarr.GetUpperBound(0)

            For i As Integer = 0 To intupper / 2
                If strarr(i) <> strarr(intupper - i) Then
                    txtyesorno.Text = " It's not  a Palindrome"
                    Return False

                Else
                    txtyesorno.Text = "  Palindrome"

                End If


            Next

            Return True


        End Function

      Private Sub btnclear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnclear.Click
            'clears all the fields
            txtword.Clear()
            txtsentence.Clear()
            txtyesorno.Clear()
            txtword.Focus()
            intindex = 0
            
                End Sub

        Private Sub btnpalindrome_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnpalindrome.Click
            'processing if its palindrome or not

            If ispal(strarr) Then

            End If






           


        End Sub

    End Class

    mariles spencer
    • Edited by mariles Monday, November 28, 2011 6:20 PM
    Monday, November 28, 2011 6:06 PM

Answers

  • I took most of your code, but for the changes... i had added comments to it.

        Dim strword As String
        Dim intindex As Integer
        Dim strarr() As String
     
        Private Sub btnnext_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnnext.Click
     
            If Not isgoodinput(strword) Then
                Exit Sub
            End If
     
            ReDim Preserve strarr(intindex)
     
            strarr(intindex) = strword.Trim 'Added the Trim in case user enter a space
            intindex += 1
     
            txtsentence.Text += " " + strword 'added a space to show in the sentense
            txtsentence.Text = txtsentence.Text.Trim 'added Trim to end to reflect no space in beginning or end of the text.
    
     
            txtword.Clear()
            txtword.Focus()
     
        End Sub
     
        Private Function isgoodinput(ByRef word As StringAs Boolean
            If IsNumeric(txtword.Text) Or (txtword.Text) = "" Then
                MsgBox("Please enter a word.", , "Input Error")
                txtword.Focus()
                Return False
            Else
                word = txtword.Text
                Return True
            End If
        End Function
     
        Private Function ispal() As Boolean 'removed your 'strarr() As String' parameter... this is not needed, because it is already in the upper level scope of this function
     
            Dim intupper = strarr.GetUpperBound(0)
            Dim bPalindrome As Boolean = True 'declared Boolean and set it as default True value
            For i As Integer = 0 To intupper / 2
     
                bPalindrome = strarr(i) = strarr(intupper - i) 'if the 'strarr(i) = strarr(intupper - i)' then it sets it as False
     
                If Not bPalindrome Then txtyesorno.Text = " It's not  a Palindrome" : Return False 'if the bPalindrome value is False, then it sets text in the 'txtyesorno' and return the False value. Basically if ever this value is False (bPalindrome), it exits this function.
     
                'Note: Commented your original code, not needed
                'If strarr(i) <> strarr(intupper - i) Then
                '    txtyesorno.Text = " It's not  a Palindrome"
                '    Return False
     
                'Else
                '    txtyesorno.Text = "  Palindrome"
     
                'End If
            Next
     
            txtyesorno.Text = "  Palindrome"
     
            Return True
     
        End Function
     
        Private Sub btnclear_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnclear.Click
            txtword.Clear()
            txtsentence.Clear()
            txtyesorno.Clear()
            txtword.Focus()
            intindex = 0
        End Sub
     
        Private Sub btnpalindrome_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnpalindrome.Click
            
     
            If ispal() Then 'no need to pass the 'strarr' since it is in the uppers scope and redifined the procedure not to include any parameter.
                MessageBox.Show("You entered a Palindrome"'????????????????
            End If
     
        End Sub



    • Edited by RoninB Monday, November 28, 2011 9:42 PM
    • Marked as answer by mariles Monday, December 5, 2011 12:55 AM
    Monday, November 28, 2011 9:36 PM

All replies

  • I took most of your code, but for the changes... i had added comments to it.

        Dim strword As String
        Dim intindex As Integer
        Dim strarr() As String
     
        Private Sub btnnext_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnnext.Click
     
            If Not isgoodinput(strword) Then
                Exit Sub
            End If
     
            ReDim Preserve strarr(intindex)
     
            strarr(intindex) = strword.Trim 'Added the Trim in case user enter a space
            intindex += 1
     
            txtsentence.Text += " " + strword 'added a space to show in the sentense
            txtsentence.Text = txtsentence.Text.Trim 'added Trim to end to reflect no space in beginning or end of the text.
    
     
            txtword.Clear()
            txtword.Focus()
     
        End Sub
     
        Private Function isgoodinput(ByRef word As StringAs Boolean
            If IsNumeric(txtword.Text) Or (txtword.Text) = "" Then
                MsgBox("Please enter a word.", , "Input Error")
                txtword.Focus()
                Return False
            Else
                word = txtword.Text
                Return True
            End If
        End Function
     
        Private Function ispal() As Boolean 'removed your 'strarr() As String' parameter... this is not needed, because it is already in the upper level scope of this function
     
            Dim intupper = strarr.GetUpperBound(0)
            Dim bPalindrome As Boolean = True 'declared Boolean and set it as default True value
            For i As Integer = 0 To intupper / 2
     
                bPalindrome = strarr(i) = strarr(intupper - i) 'if the 'strarr(i) = strarr(intupper - i)' then it sets it as False
     
                If Not bPalindrome Then txtyesorno.Text = " It's not  a Palindrome" : Return False 'if the bPalindrome value is False, then it sets text in the 'txtyesorno' and return the False value. Basically if ever this value is False (bPalindrome), it exits this function.
     
                'Note: Commented your original code, not needed
                'If strarr(i) <> strarr(intupper - i) Then
                '    txtyesorno.Text = " It's not  a Palindrome"
                '    Return False
     
                'Else
                '    txtyesorno.Text = "  Palindrome"
     
                'End If
            Next
     
            txtyesorno.Text = "  Palindrome"
     
            Return True
     
        End Function
     
        Private Sub btnclear_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnclear.Click
            txtword.Clear()
            txtsentence.Clear()
            txtyesorno.Clear()
            txtword.Focus()
            intindex = 0
        End Sub
     
        Private Sub btnpalindrome_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnpalindrome.Click
            
     
            If ispal() Then 'no need to pass the 'strarr' since it is in the uppers scope and redifined the procedure not to include any parameter.
                MessageBox.Show("You entered a Palindrome"'????????????????
            End If
     
        End Sub



    • Edited by RoninB Monday, November 28, 2011 9:42 PM
    • Marked as answer by mariles Monday, December 5, 2011 12:55 AM
    Monday, November 28, 2011 9:36 PM
  • Thank you so much RoninB.  I greatly appreciate your help. i tried the code just now but its still giving me bad answer. everytime i input a word and test it, it says it's a palindrome though the word is not a palindrome. it works on a sentence though but if i input only one word , it gives me wrong answer.

    mariles spencer
    • Edited by mariles Tuesday, November 29, 2011 5:21 AM
    Tuesday, November 29, 2011 4:44 AM
  • are you wanting to check the entered value itself as palindrome?

    Sample Data:

        civic

    or

        radar

     

    in the above cases, it should be... just for verification; is this what you wanted run a check on?

    also... looking at definition of Palindrome... i think i got an idea of how the sentence phrase should be. i'll recode that as well... but please advice on word sample above.
    • Edited by RoninB Tuesday, November 29, 2011 7:18 AM
    Tuesday, November 29, 2011 7:15 AM
  • To check for a palindrome, you can use this code

     

            Dim Teststring As String = "Radar"
    
            If New String(Teststring.ToLower.Reverse.ToArray) = Teststring Then
                MsgBox("Is palindrome")
            Else
                MsgBox("Is not palindrome")
            End If
    

     

    Tuesday, November 29, 2011 11:01 AM
  • yap. the program should test the word itself or  sentence if its a palindrome or not.  even if its not  palindrome it still give an output as palindrome . if i input a sentence, it works alright. its just the one word input that does'nt work. thank you.
    mariles spencer
    Tuesday, November 29, 2011 1:16 PM
  • thanks  Crazypennie,  i'll try this code.

    mariles spencer
    Tuesday, November 29, 2011 1:19 PM
  • mariles,

    I had been occupied and seems someone has assisted you.

    additionally: the whole gui design concept might have to be reworked to what you want to accomplish.

    The word entry, by your code, enters the data in the sentence box, however by what you described, it collides with checking both sentence and word into one box (txtyesorno) for verification.

    so... if the sentence box is not Palindrome and word is, and what you wanted is to check both, one or the other will give you invalid result.


    either that or elaborate in detail of the flow of user process.
    • Edited by RoninB Thursday, December 1, 2011 5:45 PM
    Thursday, December 1, 2011 5:45 PM
  • hi RoninB, you're right the program is intended to test for a sentence not a word, that's why i had a difficult time figuring the code. i misunderstood what the professor told us. anyway, thanks so much for your time.

    mariles spencer
    Friday, December 2, 2011 4:45 AM
  • Mariles did you ever figure out how to do this lab?  I am very confused!!
    Tuesday, December 4, 2012 6:13 PM