VB中数组中的字母频率

时间:2016-04-03 21:20:19

标签: arrays visual-studio loops frequency

我需要帮助试图弄清楚如何循环一个不使用LINQ的文本文件填充的数组,并找到并显示字母的频率。

这是我给出的伪代码:

Read the words.txt file into a string array 

Declare an array of integers to hold 26 elements, call it alphabet 

Loop through the string array 

Loop through each letter in the an element of the string array  

 If the letter is not equal to “ “ then  

 Set variable ascLetter = ASC(Letter) – 65 

Add 1 to the element in alphabet(ascLetter) 

   End if 

 End loop  
End loop  

Print out each element in the alphabet array in the form of the
letter followed by hyphen then quantity

这是我到目前为止所做的:

   Dim alphabet(25) As Integer

   Private Sub btnLetter_Click(sender As Object, e As EventArgs)Handles btnLetter.Click

    Dim words() As String = IO.File.ReadAllLines("E:\words.txt")

    For i = 0 To words.Count - 1
        'Loop through the letters of the file 
        For position As Integer = 0 To words(i).Length - 1
            letter = words(i)
            ascletter = (Asc(letter) - 65)
            alphabet(ascletter) += 1
            If letter <> " " Then
                lstLetters.Items.Add(Chr(position + 65) & " " & alphabet(ascletter))
            End If
        Next
    Next

0 个答案:

没有答案