[Solved] calculate frequency of each letter in a string


There are two problems here. First, while std::string is null-terminated (required in C++11, de facto in most implementations before that), you cannot access past size(). If you used string::at() directly, then you’d get hit:

reference at(size_type pos);
     Throws: out_of_range if pos >= size()

which would be true for the null terminator. So the right way to iterate over a std::string is either the C++11 way:

for (value c : text) { ... }

or the C++03 way:

for (size_t i = 0; i < text.size(); ++i) {
    value = text[i];
    ...
}

You don’t need to walk until you hit '\0.

The second problem is your terminal loop:

j=0 ;
//displaying the frequencies of each character 
while(j<6)
{
    cout << freq[j] << endl ;
}

It won’t terminate. This is a good reason to prefer to use for loops:

for (j=0; j < 6; ++j) 
//               ^^^^ you were missing this
{
    cout << freq[j] << endl ;
}

1

solved calculate frequency of each letter in a string