String class methods

bmoore

Member
Joined
Feb 1, 2010
Messages
6
Programming Experience
Beginner
input = "my mother is a great lady"
output = A -3, B - 0, C - 0, D - 1, E - 2

I wanted to count the number of times letter A - E is in the input srting.
How would i do that?
 
Sounds like homework. You need to do the work so you can learn it, here are some pointers:

1) you need to convert the string into an array of Char,
2) loop thru the array and count how many you find of each letter you are looking for.
 
Strings are Char arrays.

VB.NET:
Dim characters() As Char = "my mother is a great lady"
For Each c As Char In characters
  'look for the letters
Next c
 
What you need next are variables for counting.
1 for each letter you are looking for, then when you find a letter add 1 to that integer variable. You will need some logic like a If/Else statement or Select Case (my preference) inside your loop.
 
As of now were just learning the string methods and properties. I need to count and display ther number of occurrences of letters a,b,c,d,e. by using string method and properties. I know i have to execute a do-while and select case but i just don't know what method to use.
 
Here is one way of doing it, counting one letter. You figure out how to count the rest:

sentence = "My mother is a great lady"
ch = "a"
count = 0
For x = 0 To sentence.Length - 1
If sentence.Substring(x, 1) = ch Then
count = count + 1 'same as count += 1
End If
Next x
 
VB.NET:
Dim target = "abcde".ToCharArray
Dim input = "my mother is a great lady".ToCharArray
Dim results = From x In target Select New With {.Character = x, .Count = input.Count(Function(y) y = x)}
For Each result In results
    With result
        Debug.WriteLine(String.Format("{0} : {1}", .Character, .Count))
    End With
Next
 
Back
Top