13,087,235 members (64,854 online)
Rate this:
Please Sign up or sign in to vote.
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
Comments
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

## 6 solutions

Rate this:
Please Sign up or sign in to vote.

## 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 ;)
Comments
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:
Please Sign up or sign in to vote.

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

charList.Add(message[j]);
charCountList.Add(charCount);

/* reset charCount */
charCount = 0;
}
}```
Regards,
v3
Rate this:
Please Sign up or sign in to vote.

## 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 input = Console.ReadLine();
string output = CountAllCharecters(input);
Console.WriteLine(output);
Console.ReadLine();
}
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;
}
}
}```
Comments
dasblinkenlight 21-Jan-11 12:49pm

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:
Please Sign up or sign in to vote.

## 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.
Comments
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:
Please Sign up or sign in to vote.

## 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:
Please Sign up or sign in to vote.

## Solution 3

Maybe you should ask your professor for help with your homework.
Comments
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Jochen Arndt 260 RyanDev 218 OriginalGriff 166 Karthik Bangalore 120 Graeme_Grant 120
 OriginalGriff 3,123 Graeme_Grant 1,529 ProgramFOX 1,377 Jochen Arndt 1,300 ppolymorphe 1,257

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 22 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100