tags:

views:

343

answers:

6

I'm a total noob as far as visual basic goes. In class I need to do the following: "Write a program that requests a sentence from the user and then records the number of times each letter of the alphabet occurs."

How would I go about counting the number of occurrences in the string?

Thanks and sorry for the noob question.

+4  A: 

Loop the chars and add to the list

Dim s As String = "tafata"
Dim count As New Dictionary(Of Char, Integer)()
For i As Integer = 0 To s.Length - 1
    count(s(i)) = (If(count.ContainsKey(s(i)), count(s(i)) + 1, 1))
Next

Havent worked with linq that mutch, but i think you can try

Dim s As String = "tafata"
Dim t = From c In s _
Group c By c Into Group _
Select Group
astander
Haven't had time to try it yet, but thanks for the code.
Dylan
Question is tagged vb, this code is C#.
ephemient
A: 

I would count and remove every instance of the first character, and repeat until there are no characters left. Then display the count for each character detected. Much better than looping through once for every possible character, unless you know the possible range of characters is smaller than the length of the sentence.

Ross
A: 
Imports System.Linq
Dim CharCounts = From c In "The quick brown fox jumped over the lazy dog." _
                 Group c By c Into Count() _
                 Select c, Count
ephemient
A: 
from itertools import groupby
sentence = raw_input("Your sentence:")
for char,group in groupby(sorted(sentence.lower())):
    print(char+": "+`len(list(group))`)
Phil H
+1  A: 

As Dave said, the easiest solution would be to have an array of length 26 (for English), and loop through each character in the string, incrementing the correct array element. You can use the ASCII value of each character to identify which letter it is, then translate the ASCII number of the letter into the corresponding index number:

'Dimension array to 26 elements
Dim LetterCount(0 to 25) As Long

'Temporary index number
Dim tmpIdx As Long

'Temporary character
Dim tmpChar as String

'String to check
Dim checkStr As String
checkStr = "How many of each letter is in me?"

'Change all letters to lower case (since the upper case
'of each letter has a different ASCII value than its
'lower case)
checkStr = LCase(checkStr)

'Loop through each character
For n = 1 to Len(checkStr)

  'Get current character
  tmpChar = Mid(checkStr, n, 1)

  'Is the character a letter?
  If (Asc(tmpChar) >= Asc("a")) And (Asc(tmpChar) <= Asc("z")) Then

    'Calcoolate index number from letter's ASCII number
    tmpIdx = Asc(tmpChar) - Asc("a")

    'Increase letter's count
    LetterCount(tmpIdx) = LetterCount(tmpIdx) + 1

  End If

Next n

'Now print results
For n = 0 to 25
  Print Chr(Asc("a") + n) & " - " & CStr(LetterCount(n))
Next n
Sinusoid
A: 

The quick and dirty way:

Public Function CountInStr(ByVal value As String, ByVal find As String, Optional compare As VbCompareMethod = VbCompareMethod.vbBinaryCompare) As Long
    CountInStr = (LenB(value) - LenB(Replace(value, find, vbNullString, 1&, -1&, compare))) \ LenB(find)
End Function
Oorang