4

文字列内のすべての文字をカウントする関数を C++ で書きたいと思います。 :

string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};

文字の出現頻度を格納するために使用されるベクトル。たとえば、A は 0 の位置にあり、B は 0 の位置にあるなどです。

vector<long> letterCount (26);

私はそれが機能するはずだと思うように関数を書きました.文字の出現を把握できるようですが、その後、この数字はアルファベットの文字の場所で乗算されます. 関数は次のとおりです。

long countLetters(int& p) {
  for(int i = 0; i < alphabet.size(); ++i) {
      for(long j = 0; j < count(input.begin(), input.end(), alphabet.at(i)) {
          countLetters.at(i)++;
      }
  }
return letterCount.at(p);
}

たとえば、入力が「HELLO」の場合、プログラムは次のように出力します。

E : 5
H : 8
L : 24
O : 15

たとえば、文字 'L' は文字列に 2 回含まれていますが、'L' はアルファベットの 12 番目にあるため、'L' の結果は 24 になります。

私の問題が何であるかを理解したら、助けてください。

編集:少なくとも部分的に機能する方法を見つけました:

long countLetters(int& p) {
   for(size_t i = 0; i < input.length(); ++i) {
      for(size_t j = 0; j < alphabet.length(); ++j) {
        letterCount.at(j) = count(input.begin(), input.end(), alphabet.at(j));
      }
   }
   return letterCount.at(p);
 }

しかし、2 つ以上の単語を入力すると、関数は最初の単語の文字出現のみを計算します。より多くの単語を分析するにはどうすればよいですか?

編集:私が持っていた前ですがcin >> inputgetline(cin, input);正しいです。

4

10 に答える 10

4

ある種の奇妙な二重ループを実行しています。代わりに、単一のループで文字列を反復処理し、正しいグループでカウントします。

for (int i = 0; i < input.length(); i++) {
    char c = input[i];
    if (c < 'A' || c > 'Z') continue;
    countLetters[c-'A'] += 1;
}
于 2013-07-13T09:11:15.327 に答える
1

これは、問題を解決し、結果を降順で出力する私のバージョンです。

void printNumofLetterinString(std::string sentence){
    int frequencyArray[26];         //FrequencyArray is used to store the frequency
    for(int i=0;i<26;i++){          //of the letters and Initialize 
        frequencyArray[i] = 0;      //frequencyArray to all zero.
    }
    int ascii;
    for(int i=0;i<sentence.length();i++){
        if(!isalpha(sentence[i])){
            continue;
        }
        ascii = tolower(sentence[i]) - 'a';   //Convert A-Za-z to number between 0-25.
        frequencyArray[ascii]++;
    }
    for(int i=0;i<26;i++){              //Find the biggest number in frequencyArray     
        int max = frequencyArray[0];    //print it, then set it to zero  
        int index = 0;                  //and find the next biggest number.
        for(int j=0;j<26;j++){
            if(frequencyArray[j] > max){
                max = frequencyArray[j];
                index = j;
            }
        }
        if(max == 0){
            break;
        }
        char c = index + 'a';
        std::cout<<c<<" "<<max<<std::endl;
        frequencyArray[index] = 0;
    }
}

結果は次のようになります

input caaabb
output a 3
       b 2
       c 1
于 2014-10-25T17:45:34.363 に答える
0

これを行うこともできます:

 char   *x = "cmnasdkASFSAFASDisdajkhasdfjqwedz" ; // work UPPER , lower
 static int  c[26] ;

 int main ( void ) {

 while ( *x )   {
     int ndx= *x - (islower(*x) ? 'a' : 'A') ;
     c[ ndx] += isalpha(*x++) ? 1 : 0 ;
 }
 for ( int i =0;i<26;i++)   printf ( "\n%d", c[i] );  
}
于 2013-07-13T09:58:09.833 に答える
0
#include<iostream>
#include <conio.h>
using namespace std;

int main (){
char str[50];
cin.getline(str,50);
int arr[1234]={0};

///extraction of every character 
int i=0;
while(str[i]!='\0'){

arr[str[i]-' ']++;    /* converting characters into integer type implicitly 
                       and storing freq of the ASCII characters at that 
                        position of the array.' ' space char is just a 
                        reference point... */ 


i++;
}



///show character freq
for (i=0;i<256;i++) {
if (arr[i]!=0)
cout <<"letter "<<char(i+' ')<<"  is present "<<arr[i]<<" times "<<endl;

}

return 0;

}

/* the arr array contains freq of all the characters and symbols occuring in 
a string after ' '(space) character ..so beware of entering the characters 
that are before ' ' on the standard ASCII table and your program should run 
fine..if you want to do so just replace the ' ' everywhere with the first 
character of the ASCII table.....*/
于 2017-11-01T19:38:50.977 に答える