Character frequency in array

I am having problems counting the frequency of all characters in a given array. This is my code so far. For some reason, all it outputs is 0. What should i change? I can't see the problem as i think i am incrementing d the right way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int f[26]={0};
    char text[200];
    cin.get(text, 200);
    char i;
    for(i=0;i<strlen(text);i++){
        int d=i-'a';
        f[d++];
    }

    for(int j=0;j<26;j++){
        cout<<char(j+'a')<<" apare de "<<f[i]<<endl;
    }
    return 0;
}
closed account (28poGNh0)
Try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# include <iostream>
# include <cstring>
using namespace std;

int main()
{
    int f[26]={0};
    char text[200];

    cin.get(text, 200);
    size_t i;

    for(i=0;i<strlen(text);i++)
    {
        cout << text[i]-'a' << endl;
        f[text[i]-'a']++;
    }

    for(int j=0;j<26;j++)
    {
        cout << char(j+'a') << " apare de " << f[j] <<endl;
    }
    
    return 0;
}


Hope that helps
Yep, it works! Thanks!
Topic archived. No new replies allowed.