문자열을 반복하고 각 문자의 수를 계산하는 프로그램을 만들려고합니다. 문제는 배열을 제대로 저장할 수 없다는 것입니다. 어떤 도움이라도 대단히 감사합니다.배열이 제대로 업데이트되지 않습니다.
int main()
{
string textRad = "";
int histogram[ANTAL_BOKSTAVER];
getline(cin, textRad);
berakna_histogram_abs(histogram, textRad);
cout << histogram[0] << endl;
cout << histogram[2];
return 0;
}
void berakna_histogram_abs(int histogram[], string textRad)
{
for(int i = 0; i < ANTAL_BOKSTAVER; i++)
{
histogram[i] = 0;
}
for(int i = 0; i < textRad.length(); i++)
{
for(int j = 0; j < ANTAL_BOKSTAVER; j++)
{
int antal = 0;
string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if(char(toupper(textRad.at(i))) == alfabet.at(j))
{
antal++;
}
histogram[j] = antal;
}
}
}
J의 각 반복에,'antal'는 0에서 시작됩니다. –