12,406,986 members (58,378 online)
Rate this:
See more:
Provide a method that when passed a string will return the count of every alpha character.

suppose if i take string=aabbc
o/p: a's=2
b's=2
c=1

has to come pls any one help me
Posted 20-Jan-11 10:28am
Updated 20-Jan-11 10:35am
Not Active10.2K
v2
Mark Nischalke 20-Jan-11 16:35pm

We are not here to do your homework. Show what you have tried and ask a question about.
Sandeep Mewara 21-Jan-11 0:43am

What kind of help? Have you made any effort from your side?
rupa1986 21-Jan-11 11:25am

help insense i mean to get the best of all solutions not that im lazy to do more over its not my homework it is the question asked by an employer buddies:)can check my answer posted...
CPallini 21-Jan-11 12:38pm

It is just a basic task, you should enjoy yourself finding your own solution.
RudolfErasmus 24-Jan-11 2:06am

I gave you a 5 for taking the initiative for seeking help on a public forum.
Its a good habit of getting into. But more so for a "if all else fails" matter. Hence the valid comments you got from everyone. ;P

Rate this:

## Solution 2

If you have not studied dictionaries yet, recall that `char` values can be used as an array index (it's simply a number between 0 and char.MaxValue). If you create an array of counters, you'd be able to run through the characters in your string with a `foreach` loop, incrementing your counters along the way. Then you can print your result by going through the counters, and printing non-zero values (character code equals the index of the counter).

P.S. If you just need an answer, try this:
`var res = str.Distinct().ToDictionary(c => c, c => str.Where(a => a == c).Count());`
It's a correct solution, but don't be surprised if your teacher suspects something
Söderlund 25-Jan-11 3:02am

Haha yeah,If you show a snippet with flawless lambda expressions your teacher might want you to write that again in front of him.
Rate this:

## Solution 4

Here is a start to what you can do,
Its not complete and syntax might be lacking but I gotto work now.

```void CountStringLetters(string message, list<char> charList, list<int> charCountList)
{
int len = message.Length();
char searchChar;
int charCount;

for (int i = 0; i < len; i++)
{
searchChar = message[i];

for (int j = 0; j < len; j++)
{
if (message[j] == searchChar)
{
charCount = charCount + 1;
}
}

/* reset charCount */
charCount = 0;
}
}```
Regards,
v3
Rate this:

## Solution 5

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Assignment
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a character string");
string output = CountAllCharecters(input);
Console.WriteLine(output);
}
static string CountAllCharecters(string strn)
{
string strresult = string.Empty;
try
{
int i = 0;
//strn = strn.Replace(" ", "");
strn = Regex.Replace(strn, @"[\d\s@#%\$^&*().,?!_]", "");
i = strn.Length;```
```while (i > 0)
{
string substr = strn[0].ToString();
strn = strn.Replace(substr, "");
strresult = strresult + substr.ToUpper() + " occured " + (i - strn.Length).ToString() + " times \n";
i = strn.Length;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return strresult;
}
}
}```

A good program most of the time makes it easy to answer a simple question: "what does this code do?" Trying to figure out the purpose of your piece of code is a puzzle in its own right.

You don't have to take my word for it: remove the words "occurred" and "times" from your code, show the result to your friends, and ask them to tell you what your code does.
Rate this:

## Solution 6

Hello Rupa,
Your professor here. I gave the assignment to do it yourself, not to re-assign it to others. I need to see you in the classroom.
SAKryukov 21-Jan-11 16:40pm

Right! And give him a final warning.
And don't forget to change the assignment.
(My 5)
orc_orc_orc 22-Feb-11 15:09pm

is this for real? if so thats a real fail for Rupa and he needs a lesson to learn.
Yusuf 22-Feb-11 15:25pm

Smile. You are in candid camera. :-)
Rate this:

## Solution 1

Create a
Dictionary<char,int>

Take a look at Char.IsLetter [^]

Loop over the characters in the string and update the dictionary, use the int as your character count.

Regards
Espen Harlinn
v2
Rate this:

## Solution 3

SAKryukov 21-Jan-11 16:38pm

Some voters must say this answer is crazy? Shame on them! At least, this answer should teach them to be honest!
I vote 5 to oppose laziness and consumerism at the expense of more honest fellow students.

Top Experts
Last 24hrsThis month
 OriginalGriff 265 Karthik Bangalore 235 Richard MacCutchan 220 Afzaal Ahmad Zeeshan 75 Ruselo Riva Asentista 40
 OriginalGriff 8,027 Karthik Bangalore 3,541 ppolymorphe 3,420 Richard MacCutchan 2,652 F-ES Sitecore 2,300