Let's say I have a string string str;
, and it has any number of letters in it, and I want to count how many of each letter is in the string. For example, the word "Example"
has 2 'e'
, 1 'x'
, 1 'a'
, 1 'm'
, 1 'p'
, and 1 'l'
. Is there a more efficient way of checking for each of these letters than this?
for (int i = 0; i < str.length(); i++)
{
if (str.at(i) == 'a')
{
//variable which keeps track of a ++
}...
//25 more of that for each other letter
}
It feels like there has to be a more efficient way of doing this, but I have no idea how. Please enlighten me.