[VB.NET]統計英文字串中字母個數

今天在論壇中看到一個不算難的問題,是想要能統計出英文單字的個數。由於一開始誤解了其意思,就變成了統計英文字串中字母的個數了。既然都寫了,隨手記錄一下。這問題有很多解法,這邊隨手寫了三個,也希望大家能提供一些不一樣的寫法。

解法一

採用陣列搭配ASCII編碼來記錄出現次數


Sub Test(ByVal inputString As String)
Dim symbolCount(25) As Integer
Dim startAscii As Integer = Asc(“a”c)
Dim maxSymbolIdx As Integer
Dim idx As Integer

    For Each c As Char In inputString
        idx = Asc(c) - startAscii
        symbolCount(idx) = symbolCount(idx) + 1
        If symbolCount(idx) > symbolCount(maxSymbolIdx) Then
            maxSymbolIdx = idx
        End If
    Next

    For idx = 0 To 25
        Console.Write(Chr(startAscii + idx) & ": ")
        Console.WriteLine(symbolCount(idx).ToString)
    Next
    Console.WriteLine()
    Console.Write("Max Count Symbol: ")
    Console.WriteLine(Chr(startAscii + maxSymbolIdx))
End Sub</pre></div><p> </p><h2> </h2><h2>解法二</h2><p> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:313a96f2-0f63-4ed7-aa9d-e6b0bc28f245" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code">
Sub Test(ByVal inputString As String)
    Dim chars() As Char = inputString.ToCharArray
    Array.Sort(chars)

    Dim maxChar As Char
    Dim maxCount As Integer = 0
    Dim preChar As Char = chars(0)
    Dim count As Integer = 0
    For Each c As Char In chars
        If preChar &lt;&gt; c Then
            Console.Write(preChar &amp; ": ")
            Console.WriteLine(count.ToString)
            preChar = c
            count = 0
        End If
        count += 1
        If count &gt; maxCount Then
            maxCount = count
            maxChar = c
        End If
    Next
    Console.WriteLine()
    Console.Write("Max Count Symbol: ")
    Console.WriteLine(maxChar)
End Sub</pre></div><p> </p><p> </p><p> </p><h2>解法三</h2><p>採用Linq的Group By與Order By來做 <br /> </p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:b75086f1-b457-466e-8193-584b39920ead" class="wlWriterEditableSmartContent"><pre class="vb:nocontrols" name="code">
Sub Test(ByVal inputString As String)
    Dim chars() As Char = inputString.ToCharArray
    Dim linq = From c In chars Group c By c Into Group Order By Group.Count Descending Select New With {.Symbol = c, .Count = Group.Count}
    For Each item In linq
        Console.WriteLine(item.Symbol &amp; ": " &amp; item.Count)
    Next
    Console.WriteLine()
    Console.Write("Max Count Symbol: ")
    Console.WriteLine(linq(0).Symbol)
End Sub</pre></div>