none
Making a Pangram with Visual Basic RRS feed

  • Question

  • Pangram:

    a sentence or verse that contains all the letters of the alphabet.

    I am trying to make a pangram app in visual basic that will tell me whether a word or phrase is a pangram or not. I have the following code: 

    Public Class Form1
        Private Sub btnPangram_Click(sender As Object, e As EventArgs) Handles btnPangram.Click
            'Declare the vars
            Dim strAlphabet() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
            Dim intCounter(25) As Integer
            Dim strInput As String
            Dim strMissingLetters As String
            Dim strSingleChar As String
    
            Dim i As Integer
            Dim j As Integer
            Dim x As Integer
    
            Dim boolPangram As Boolean
            Dim strPangramNo As String = "No, it is not a Pangram "
            Dim strPangramYes As String = "Yes, it is a Pangram "
    
            'Get the input
            strInput = txtInput.Text
            strInput = strInput.Replace(" ", "") 'Replace spaces with no space
            strInput = strInput.ToLower()
    
            'Processing
    
            'For the length of the input phrase
            For i = 0 To strInput.Length - 1
                'Gets every individual character in the phrase
                strSingleChar = strInput.Substring(i, 1)
                'search for each letter in the phrase for a letter in the alphabet array
                For j = 0 To strAlphabet.Length - 1
                    'If a character is found, add 1 to the counter
                    If strSingleChar = strAlphabet(j) Then
                        intCounter(j) += 1
                    End If
                Next j
            Next i
    
            For x = 0 To 26
                If intCounter(x) = 0 Then
                    boolPangram = False
                    strMissingLetters &= strAlphabet(x)
                Else
                    boolPangram = True
                End If
            Next
    
            If boolPangram = False Then
                lblResult.Text = strPangramNo & vbNewLine &
                "Missing letters: " & strMissingLetters
            Else
                lblResult.Text = strPangramYes
            End If
        End Sub
    End Class
    

    I know the strMissingLetters is wrong, because I'm just adding all the letters from the alphabet from the alphabet array. But how do I get the letters that were not used and put them in the strMissingLetters variable? And whenever I run my code, it says index out of range at this line:

    If intCounter(x) = 0 Then

    how is the index out of range? I ran through breakpoints and it was never out of range

    Friday, September 29, 2017 1:49 AM

Answers

All replies

  • how is the index out of range? I ran through breakpoints and it was never out of range

    There are 26 letters in your alphabet.  If you count from 0 to 26 then you are expecting 27 letters, and that won't work.
    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/

    If you get the code to run past that point then you should show the text you are using for the example, the result you get in strMissingLetters and why you think it is wrong.

    • Marked as answer by Logan lees Friday, September 29, 2017 2:15 AM
    Friday, September 29, 2017 2:06 AM
  •  One of the best tools in Visual Studio is the debugger. With that I suggest you set a break-point before the issue occurs and step through the code seeking out the issue. While stepping through code there is the Local window located under the main menu, debugger, windows which can assist with this to examine variables.

    Bottom line (as you know now) that x is out of bounds as an indexer for the array.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, September 29, 2017 2:09 AM
    Moderator
  • you're right, duh haha. Then how do I get strMissingLetters to get the letters that were not used in the input and put them in the strMissingLetters variable? I don't know how I would grab every letter from the parallel array that was zero, and get the corresponding letter from it and put it in the strMissingLetters
    Friday, September 29, 2017 2:15 AM
  • I don't know how I would grab every letter from the parallel array that was zero, and get the corresponding letter from it and put it in the strMissingLetters

    What result are you getting when you fix that first error?  I suspect the problem is not with the missing letters string at all, but with the way that you are testing the array.

    Consider this:
    If 'z' is missing, the answer is different than if 'z' is there.  Why might that be?

    It is good practice to explicitly initialise every variable - integers, string and bools included - even though the compiler does a default initialization for you,

    Friday, September 29, 2017 2:22 AM
  • So, if I put this phrase in, "A quick brown fox jumped over the lazy dogs" It says it is a Pangram. Which is correct. If I put this phrase in: "A quick brown fox jumped over the lazy dog" (without the 's') the program still says the phrase is a pangram, which is not correct. I look In my breakpoints and I can see that in the intCounter(x), index 18 has a zero on it. Which is true. If that is true, it should be executing the next line of code right? which is "boolPangram = false" which tells the program that the phrase is not a pangram. Instead it goes to the next statement, Else boolPangram = True". So somehow the program is not seeing that there is a zero in the intCounter? I'm very confused, sorry 
    Friday, September 29, 2017 2:30 AM
  • So, if I put this phrase in, "A quick brown fox jumped over the lazy dogs" It says it is a Pangram. Which is correct. If I put this phrase in: "A quick brown fox jumped over the lazy dog" (without the 's') the program still says the phrase is a pangram, which is not correct. I look In my breakpoints and I can see that in the intCounter(x), index 18 has a zero on it. Which is true.

    So that array is correct - the analysis of the array is wrong.

    I mentioned 'z'.  Test it without and without a 'z'.   Then ask "In what way is 'z' different than every other letter in the alphabet?  Go for the simplest possible answer you can think of.  Then consider how that might affect your analysis of the array.

    Friday, September 29, 2017 2:37 AM
  • Well, it is the last letter.. But i still dont understand why its making my answer incorrect. because the intcounter still counts the Z.. shouldn't it still work? Sorry, I tend to overthink and Im sure that is why im not understanding
    Monday, October 2, 2017 1:09 PM
  • Well, it is the last letter.. But i still dont understand why its making my answer incorrect. because the intcounter still counts the Z.. shouldn't it still work? Sorry, I tend to overthink and Im sure that is why im not understanding

    Food for thought:

        Private Function IsPangram(ByVal wordOrPhrase As String) As Boolean
    
            Dim retVal As Boolean = False
    
            If Not String.IsNullOrWhiteSpace(wordOrPhrase) Then
                If wordOrPhrase.Trim.Length >= 26 Then
                    Dim tempList As New List(Of Char)
    
                    For i As Integer = 97 To 122
                        tempList.Add(Chr(i))
                    Next
    
                    Dim result As Boolean = True
    
                    For Each c As Char In tempList
                        If Not wordOrPhrase.Trim.ToLower.Contains(c) Then
                            result = False
                            Exit For
                        End If
                    Next
    
                    retVal = result
                End If
            End If
    
            Return retVal
    
        End Function


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, October 2, 2017 1:33 PM
  • Other thought food:

        Private ReadOnly theAlphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToArray
        Private Function isPanagram(checkThis As String) As String
            Dim rv As String = ""
            'the alphabet to a list, lists are easier to remove from
            Dim tempA As List(Of Char) = theAlphabet.ToList
            'look at each char in the textbox
            For Each c As Char In checkThis.ToLower 'lower case
                Dim idx As Integer = tempA.IndexOf(c)
                If idx >= 0 Then 'is this char in list?
                    tempA.RemoveAt(idx) 'yes, remove it
                ElseIf tempA.Count = 0 Then 'all letters removed?
                    Exit For 'yes, is panagram
                End If
            Next
            If tempA.Count <> 0 Then
                rv = String.Format("Missing: {0}", String.Join(", ", tempA))
            End If
            Return rv 'if rv = "" then checkThis is a panagram
        End Function

    to use:

            Dim m As String = isPanagram(TextBox1.Text)
            If m = "" Then
                Debug.WriteLine("is panagram")
            Else
                Debug.WriteLine(m)
            End If
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


    • Edited by dbasnett Monday, October 2, 2017 2:37 PM
    Monday, October 2, 2017 2:31 PM
  • Well, it is the last letter.. But i still dont understand why its making my answer incorrect. because the intcounter still counts the Z.. shouldn't it still work? Sorry, I tend to overthink and Im sure that is why im not understanding

    It does count the z - but that's the only letter it correctly identifies as missing.

            For x = 0 To 26
                If intCounter(x) = 0 Then
                    boolPangram = False
                    strMissingLetters &= strAlphabet(x)
                Else
                    boolPangram = True
                End If
            Next

    But if the z is not missing it takes the Else path, and the result is True.  So your result is based entirely on the absence/presence of z.  Because you have both a True part and a False path, only the last letter in the list is being tested.  That's why I suggested you run a test with and without z.

    So if a letter is not missing, that test should not set the result back to True, but should just leave it at what it already was, in case some previous letter had already set it to False.

    Monday, October 2, 2017 8:44 PM