Page 1 of 1

How do I count the number of letters in a textbox using c#?

Posted: Fri May 15, 2009 11:07 am
by Keevu
How do I count the number of letters in a textbox using c#?

I specifically need the exact number of letters. example alabama has 7 letters.
after i get that number i need the number of times each letter is used. example mississippi has 1m 4 i's 4 s's and 2 p's.

thanks for interest.

Re: How do I count the number of letters in a textbox using c#?

Posted: Fri May 15, 2009 11:14 am
by MarauderIIC

Code: Select all

myTextBox.Text.Length
gets the string length. If you want letters as opposed to numbers and spaces, you'll have to test to see if it's a letter. I think iterating through as below and testing c.IsLetter() might do it.

Then to count frequency, I think a Dictionary would be the best way. You would go through the string character by character using something like

Code: Select all

System.Collections.Generic.Dictionary myCollection = new Dictionary<char, int>();
foreach (char c in myTextBox.Text) {
    if (myCollection[c] == null)
        myCollection[c] = 1
    else
        myCollection[c]++;
}
should work. Didn't test, so it probably doesn't work, but you should get the idea.

Please edit your topic title and add [SOLVED] on the front if this does it for you.