Click here to Skip to main content
15,069,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Given a string such as "basheer", how would I could all the given instances of the characters in the string and output it? The sample I'm after is:


Updated 26-Apr-12 22:22pm
Paul E Davies 27-Apr-12 4:07am
What have you tried? Why didn't that work?

A simple way to achieve this would be to use a Dictionary<char, int> which represents the letter and the count of instances. Start at the left character and work your way towards the last character. With each character, check to see if it's already in the dictionary. If it is increment the count. If it isn't, add the character in and set the count to 1.

There are other ways to achieve this, of varying complexity, but remember that you are going to have to support this in the future, and this looks to be a homework question so don't worry about the performance too much. The important thing with homework is clarity of code, and demonstrating that you understand the principals - this is the whole reason I've just told you what the logic is that you would apply, and haven't actually given you the code; that is left to you to prove that you do understand the principals.
VJ Reddy 27-Apr-12 4:34am
Good guidance. 5!
slightrain 27-Apr-12 4:39am
That's very good, but for the performance, should use IDictionary replace Dictionary object.
Sergey Alexandrovich Kryukov 27-Apr-12 23:04pm
Sorry, this is wrong. It has nothing to do with performance. The run-time type of IDictionary will be Dictionary anyway, of one of other types implementing IDictionary.
Sergey Alexandrovich Kryukov 27-Apr-12 23:02pm
Good, my 5.
The GroupBy extension method and Count extension methods of IEnumerable interface can be used as shown below:

void Main()
	string str="basheer";
	List<string> charCounts = str.GroupBy (ch => ch).Select (
			item => item.Key + "=" + item.Count().ToString()).ToList();
	foreach (string  s in charCounts)
		Console.WriteLine (s);		
Pete O'Hanlon 27-Apr-12 4:21am
It's good, but as it looks to be homework, it might actually be too good. That's why I suggest he users a simpler method. No offence mate, but you've just done his thinking for him, and I don't think you're going to get the mark from the tutor.
VJ Reddy 27-Apr-12 4:33am
Thank you for the comment. I did not think in the direction of homework. You're correct. I have seen your answer. It's very good guidance.
Pete O'Hanlon 27-Apr-12 4:48am
No problem, I obviously have a nastier, more suspicious mind than you;)
Sergey Alexandrovich Kryukov 27-Apr-12 23:06pm
I basically tend agree with you here, Pete, but it does not prevent me from voting 5 for VJ's solution. Isn't that fair enough?
VJ Reddy 27-Apr-12 23:18pm
Thank you very much for the consideration, SA
Pete O'Hanlon 28-Apr-12 3:06am
I had already voted 5 because it was a good solution. My comment was just to let him know that it was too good for a homework solution. The tutor is going to know that this solution was arrived at by a professional coder. If the op had been able to come up with a solution as neat as this, he wouldn't have had to ask the question in the first place.
VJ Reddy 28-Apr-12 3:09am
Thank you, Pete O'Hanlon.
Sergey Alexandrovich Kryukov 30-Apr-12 19:37pm

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900