Frequency of letters finder

Wolfin

New member
Joined
Oct 31, 2006
Messages
1
Programming Experience
Beginner
My Programming 12 class is working on a Letter Counter that counts the occurence of all letters (and spaces) in a string. All of us are stumped as to how this code isn't working. We don't know too much about arrays, so we tried it with variables and it still didn't work. Is anyone able to help us with an answer?

Thanks!

VB.NET:
Dim myString(26) As Integer
    Dim i As Integer = 0


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim inny As String = Input.Text
        Dim added As Integer = (i + 65)
        Dim counter As Integer
        Dim substring As Char

        inny = inny.Trim()
        inny = inny.ToUpper

        Output.Text = ""

        For counter = 0 To inny.Length   'goes for length of input string
            For i = 0 To 26        'goes through each letter
                substring = inny.Substring(i, 1)
                If substring = Chr(added) Then  'takes character in the position of i and if it equals the character code equivalent to i + 65
                    myString(i) = myString(i) + 1   'it adds one to its array
                End If
                If substring = Chr(32) Then    'if space
                    myString(26) = myString(26) + 1       'add one to myString spot for spaces
                End If
            Next
        Next

        Output.Text = myString(0) & " A's" & vbNewLine & myString(1) & " B's" & vbNewLine & _
 myString(2) & " C's" & vbNewLine & myString(3) & " D's" & vbNewLine & _
 myString(4) & " E's" & vbNewLine & myString(5) & " F's" & vbNewLine & _
 myString(6) & " G's" & vbNewLine & myString(7) & " H's" & vbNewLine & _
 myString(8) & " I's" & vbNewLine & myString(9) & " J's" & vbNewLine & _
 myString(10) & " K's" & vbNewLine & myString(11) & " L's" & vbNewLine & _
 myString(12) & " M's" & vbNewLine & myString(13) & " N's" & vbNewLine & _
 myString(14) & " O's" & vbNewLine & myString(15) & " P's" & vbNewLine & _
 myString(16) & " Q's" & vbNewLine & myString(17) & " R's" & vbNewLine & _
 myString(18) & " S's" & vbNewLine & myString(19) & " T's" & vbNewLine & _
 myString(20) & " U's" & vbNewLine & myString(21) & " V's" & vbNewLine & _
 myString(22) & " W's" & vbNewLine & myString(23) & " X's" & vbNewLine & _
 myString(24) & " Y's" & vbNewLine & myString(25) & " Z's"



    End Sub
 
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] inny [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "This is my test"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] added [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = (i + 65)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] counter [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] substring [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Char
[/COLOR][/SIZE][SIZE=2]inny = inny.Trim()
inny = inny.ToUpper
Output.Text = ""
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] counter = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] inny.Length - 1 [/SIZE][SIZE=2][COLOR=#008000]'goes for length of input string
[/COLOR][/SIZE][SIZE=2]substring = inny.Substring(counter, 1)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] substring = Chr(32) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#008000]'if space
[/COLOR][/SIZE][SIZE=2]myString(26) = myString(26) + 1 [/SIZE][SIZE=2][COLOR=#008000]'add one to myString spot for spaces
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#008000]'takes character in the position of i and if it equals the character code equivalent to i + 65
[/COLOR][/SIZE][SIZE=2]myString(Asc(substring) - 65) = myString(Asc(substring) - 65) + 1 [/SIZE][SIZE=2][COLOR=#008000]'it adds one to its array
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]Output.Text = myString(0) & " A's" & vbNewLine & myString(1) & " B's" & vbNewLine & _
myString(2) & " C's" & vbNewLine & myString(3) & " D's" & vbNewLine & _
myString(4) & " E's" & vbNewLine & myString(5) & " F's" & vbNewLine & _
myString(6) & " G's" & vbNewLine & myString(7) & " H's" & vbNewLine & _
myString(8) & " I's" & vbNewLine & myString(9) & " J's" & vbNewLine & _
myString(10) & " K's" & vbNewLine & myString(11) & " L's" & vbNewLine & _
myString(12) & " M's" & vbNewLine & myString(13) & " N's" & vbNewLine & _
myString(14) & " O's" & vbNewLine & myString(15) & " P's" & vbNewLine & _
myString(16) & " Q's" & vbNewLine & myString(17) & " R's" & vbNewLine & _
myString(18) & " S's" & vbNewLine & myString(19) & " T's" & vbNewLine & _
myString(20) & " U's" & vbNewLine & myString(21) & " V's" & vbNewLine & _
myString(22) & " W's" & vbNewLine & myString(23) & " X's" & vbNewLine & _
myString(24) & " Y's" & vbNewLine & myString(25) & " Z's"
[/SIZE]
 
Is this in VB.NET or VB6? I'm assuming .NET, which is probably why my answer is far shorter:
VB.NET:
Private Function CountLetters(ByVal Input As String)
    Dim intLetterCount(26) As Integer
    Dim CurrentChar As Char
    Dim Summary As String = ""

    For LetterCount As Integer = 0 To Input.Length - 1
        CurrentChar = Input.Chars(LetterCount)

        If (CurrentChar >= "A"c And CurrentChar <= "Z"c) Or _
            (CurrentChar >= "a"c And CurrentChar <= "z"c) Then
            If CurrentChar >= "a"c Then
                intLetterCount(Asc(CurrentChar) - 96) += 1
            Else
                intLetterCount(Asc(CurrentChar) - 64) += 1
            End If
        End If
    Next LetterCount

    For Alphabet As Integer = 1 To 26
        Summary &= intLetterCount(Alphabet).ToString & " " & Chr(Alphabet + 64) & "'s" & ControlChars.NewLine
    Next Alphabet

    Return Summary
End Function
 
A little shorter, strong typing the function return value, using string format for readability, using StringBuilder to build the string.
VB.NET:
    Private Function CountLetters(ByVal Input As String) As String
        Dim Counter(26) As Integer, current As Integer
        'count
        For Each c As Char In Input.ToUpper
            current = Asc(c)
            If current >= 65 AndAlso current <= 90 Then Counter(current - 64) += 1
        Next
        'present
        Dim format As String = "{0} {1}'s{2}"
        Dim summary As New System.Text.StringBuilder
        For current = 1 To 26
            summary.AppendFormat(format, Counter(current), Chr(current + 64), vbNewLine)
        Next
        Return summary.ToString
    End Function
 
Back
Top