我是 C++ 的新手,我想做的是计算一个字母与一段文本或段落一起出现的次数,并将其存储到一个称为频率数组的数组中。
下面的代码在一定程度上起作用,如果用户键入 hello frequencyarray stores 11121,如果用户键入 aaba frequencyarray stores 1213,我不想要运行总数,我希望数组存储 1121 和 31。因此,如果出现相同的字母,它会将 1 添加到数组中。
谢谢大卫
#include <iostream> //for cout cin
#include <string>   //for strings
#include <fstream>  //for files
using namespace std;
int main()
{       
    string text;
    int frequencyarray [26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    cout << "Enter Word: ";
    cin >> text;
//***************************COUNT OCCURANCES************************
    for (int i(0); i < text.length(); i++)
    {
        char c = text[i];
        c = toupper(c);
        c -= 65;
        if (c < 26 || c >=0)
        {
            frequencyarray[c]++;
            cout << frequencyarray[c];
        }
    }
    system ("pause");
return(0);
}`