Last Reply on Apr 17, 2012 12:13 AM By Mudassar

Views: 1611   Replies: 1  Answers: 1
Joined: Mar 27, 2012 11:06 PM
Location:  
delhi
India  India
Asked: 219
Answered: 19
Attempted: 62

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.

Krish


Joined: Nov 11, 2011 05:52 PM
Location:  
India 
Answered: 2697
Attempted: 3557
0

    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;
    }

 

MVP ASP.Net

© 2013 www.aspforums.net All rights reserved. Powered by Excelasoft Solutions.  Excelasoft Solutions

Disclaimer: This site is started with intent to serve the ASP.Net Community by providing forums (question-answer) site where people can help each other. The content posted here is free for public and is the content of its poster. The site does not provide any warranties for the posted content. If you feel any content is violating any terms please contact.
Google+ | FaceBook | Glossary
Google