user2307236
user2307236

Reputation: 745

Count the number of frequency for different characters in a string

i am currently tried to create a small program were the user enter a string in a text area, clicks on a button and the program counts the frequency of different characters in the string and shows the result on another text area. E.g. Step 1:- User enter:- aaabbbbbbcccdd Step 2:- User click the button Step 3:- a 3 b 6 c 3 d 1 This is what I've done so far....

    public partial class Form1 : Form
    {
        Dictionary<string, int> dic = new Dictionary<string, int>();
        string s = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            s = textBox1.Text;
            int count = 0;
            for (int i = 0; i < s.Length; i++ )
            {
                textBox2.Text = Convert.ToString(s[i]);
                if (dic.Equals(s[i]))
                {
                    count++;
                }
                else
                {
                    dic.Add(Convert.ToString(s[i]), count++);
                }
            }

        }
    }
}

Any ideas or help how can I countinue because till now the program is giving a run time error when there are same charachter!!

Thank You

Upvotes: 0

Views: 2328

Answers (4)

Guffa
Guffa

Reputation: 700152

You are attempting to compare the entire dictionary to a string, that doesn't tell you if there is a key in the dictionary that corresponds to the string. As the dictionary never is equal to the string, your code will always think that it should add a new item even if one already exists, and that is the cause of the runtime error.

Use the ContainsKey method to check if the string exists as a key in the dictionary.

Instead of using a variable count, you would want to increase the numbers in the dictionary, and initialise new items with a count of one:

string key = s[i].ToString();
textBox2.Text = key;
if (dic.ContainsKey(key)) {
  dic[key]++;
} else {
  dic.Add(key, 1);
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460018

Instead of dic.Equals use dic.ContainsKey. However, i would use this little linq query:

Dictionary<string, int> dict = textBox1.Text
    .GroupBy(c => c)
    .ToDictionary(g => g.Key.ToString(), g => g.Count());

Upvotes: 1

Valentin Kuzub
Valentin Kuzub

Reputation: 12073

var lettersAndCounts = s.GroupBy(c=>c).Select(group => new { 
                             Letter= group.Key, 
                             Count = group.Count() 
                        });

Upvotes: 2

Pepe
Pepe

Reputation: 6480

I'm going to suggest a different and somewhat simpler approach for doing this. Assuming you are using English strings, you can create an array with capacity = 26. Then depending on the character you encounter you would increment the appropriate index in the array. For example, if the character is 'a' increment count at index 0, if the character is 'b' increment the count at index 1, etc...

Your implementation will look something like this:

int count[] = new int [26] {0};

for(int i = 0; i < s.length; i++)
{
   count[Char.ToLower(s[i]) - int('a')]++;
}

When this finishes you will have the number of 'a's in count[0] and the number of 'z's in count[25].

Upvotes: 0

Related Questions