counting times each character repeats ...
Oct 2, 2010 at 7:36pm UTC
Hello everybody!
I have to count how many times each character repeats in a entered string. For example:
abcabcdekj29187a*
a ->3
b ->2
c ->2
d ->1
e ->1
etc. How can I do, please help me!
Oct 2, 2010 at 8:12pm UTC
create a variable for each character (array). iterate through string. for each character in it increment appropriate variable.
Oct 2, 2010 at 8:27pm UTC
Thanks, I've done!
This my code :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <map>
std::map<char , int > mymap;
std::map<char , int >::iterator it;
int main () {
char *str = new char [1000];
for (int i = 32; i <= 126; i ++)
mymap[i] = 0;
std::cin.sync();
std::cin.getline(str,1000);
for (int i = 0; i < strlen(str); i ++)
mymap[str[i]] += 1;
for (it = mymap.begin(); it != mymap.end(); it ++)
if (it->second != 0) printf ("%c %d\n" ,it->first, it->second);
return 0;
}
Topic archived. No new replies allowed.