2013-08-24 5 views
0

문자열을 반복하고 각 문자의 수를 계산하는 프로그램을 만들려고합니다. 문제는 배열을 제대로 저장할 수 없다는 것입니다. 어떤 도움이라도 대단히 감사합니다.배열이 제대로 업데이트되지 않습니다.

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; 
     } 
    } 
} 
+0

J의 각 반복에,'antal'는 0에서 시작됩니다. –

답변

0

는 안탈 변수를 사용하는 방법은 잘못된 것입니다. 당신은 각 편지에 대해 1을 결코 얻지 못할 것입니다. 이 같은 것이 더 잘 작동해야합니다.

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++) 
     { 
      string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

      if(char(toupper(textRad.at(i))) == alfabet.at(j)) 
      { 
       histogram[j]++; 
      } 
     } 
    } 
} 

변수 및 메소드 이름에 영어를 사용하는 것이 좋습니다. Javid의 및 Tjofras '답변 여기를 고려

0

이 시도 :

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; 

    string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    for(int i = 0; i < textRad.length(); i++) { 
     for(int j = 0; j < alfabet.length(); j++) 
      if(char(toupper(textRad.at(i))) == alfabet.at(j)) 
       histogram[j]++; 
    } 

} 
1

는 완전 간단하고 안전한 예입니다

#include <iostream> 
#include <string> 
#include <algorithm> 
#include <vector> 

void berakna_histogram_abs(std::vector<int>& histogram, const std::string& textRad); 

int main() { 

    const int ANTAL_BOKSTAVER = 26; //Assumed value. 

    std::string textRad; 
    std::vector<int> histogram(ANTAL_BOKSTAVER, 0); 

    std::getline(std::cin, textRad); 

    std::transform(textRad.begin(), textRad.end(), textRad.begin(), toupper); 
    berakna_histogram_abs(histogram, textRad); 

    std::cout << histogram[0] << std::endl; 
    std::cout << histogram[2]; 

    return 0; 
} 

void berakna_histogram_abs(std::vector<int>& histogram, const std::string& textRad) { 

    static std::string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

    std::size_t s = alfabet.length(); 
    for(std::size_t i = 0; i < s; ++i) { 
     histogram[i] = std::count(test_string.begin(), test_string.end(), alfabet[i]); 
    } 
} 
관련 문제