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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Keevu
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Mar 24, 2009 3:53 pm

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

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply