• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

Count # of times each letter in alphabet appears in a word

should output the number of times each letter in the alphabet occurs in the textbox. Case sensitivity is not required

In C#, I need to write a snippet in which the user enters a word in a textbox hit OK button and the a richtextbox displays the number of times each letter in the alphabet occurs in the textbox.
So for instance, if he enters "adD" the richtextbox would show

1 A's
0 B's
0 C's
2 D's
0 E's
0 F's

I was thinking of putting letter that user types into an array and then the alphabet in an arraylist and iterate through that. but since i'm new to c# need some assitance.
1 Solution
badtz7229Author Commented:
never mind.
alas i got it

private void button1_Click(object sender, EventArgs e)
            char[] arrAlphabet = { 'a', 'b', 'c', 'd','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
            foreach (char i in arrAlphabet)
                countOccurences(i, this.textBox1.Text.ToLower());


        public void countOccurences(char match, String s)
            int count = 0;
            char[] aString = s.ToCharArray(); 

            for (int i = 0; i < s.Length; i++) 
                if (aString[i].Equals(match))   

            this.richTextBox1.AppendText("\n" + count + " " + match.ToString().ToUpper() + "'s");

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now