Last Reply on Apr 17, 2012 12:13 AM By Mudassar
Posted on Apr 16, 2012 06:21 AM

Hi

I like to create a small program that print the number of each character and print it.

Let me more explicit.

My name is Vasanth.

And the result i expect is

V - 1

a - 2

s - 1

n - 1

t - 1

h - 1 

I need to acheive this with minimun number of loops.

Can any one suggest an idea for this.

You are viewing reply posted by: Mudassar on Apr 17, 2012 12:13 AM.
Posted on Apr 17, 2012 12:13 AM
    protected void Count(object sender, EventArgs e)
    {
        string test = "Vasanth";
        List<string> str = new List<string>();
        char[] chrs = test.ToCharArray(); 
        for (int i = 0; i < chrs.Length; i++) {
            var s = chrs[i] + " " + Occurence(test, chrs[i]);
            if (str.IndexOf(s) == -1)
            {
                str.Add(s);
            }
        } 
        Label1.Text = string.Join("<br />", str);
    }

    private int Occurence(string text, char chr)
    {
        int i = 0, count = 0;
        while ((i = text.IndexOf(chr, i)) != -1)
        {
            count++;
            i++;
        }
        return count;
    }