count letters in a string

Hello!

Can someone please help me? Im trying to make a program that would cout what letters and how much of them are in a string, for example:

string s = "aaassdaa";

and the output would be:
a = 5
s = 2
d = 1
You can create a map<char,int>, then look each character in the string and increase the value of that character in the map
http://www.cplusplus.com/reference/stl/map/
can you give me an example with string s and int a?

EDIT: i do not need exactly this, i could use a function that would show what kind characters in the string are the most
Last edited on
It would look like this:
1
2
3
4
5
6
7
string s = "whatever";
map < char, int > counter; // needs #include <map>
for ( int i = 0; i < s.size(); i++ ) // chars in the string
     counter[ s[i] ] ++;

// see http://www.cplusplus.com/reference/stl/map/begin/ for printing the results
    
Last edited on
what should i write in the for loop declaration?
I edited my previous post to real C++
ty
Topic archived. No new replies allowed.