C++が新しくなったので、ここでは私が取り組んでいるプロジェクトの一部です。文字列を使用して、最も頻繁に使用される数値と何回使用されたかを印刷します。私はこれが正しいと思ったが、何らかの理由で私のchar配列は読み込まれません。どのように修正するためのヒントや提案?文字列を文字配列に変換する
#include <string>
#include <iostream>
using namespace std;
char getMostFreqLetter(string ss);
int main() {
string s; //initilizing a variable for string s
s = ("What is the most common letter in this string "); // giving s a string
getMostFreqLetter(s); // caling the function to print out the most freq Letter
return 0;
}
char getMostFreqLetter(string ss) {
int max, index, i = 0;
int array[255] = {0};
char letters[];
// convert all letters to lowercase to make counting letters non case sensative
for (int i = 0; i < ss.length(); i ++){
ss[i] = tolower(ss[i]);
}
//read each letter into
for (int i = 0; i < ss.length(); i ++){
++array[letters[i]];
}
//
max = array[0];
index = 0;
for (int i = 0; i < ss.length(); i ++){
if(array[i] > max)
{
max = array[i];
index = i;
}
}
return 0;
}
空白を最も頻繁な手紙と考えていますか? – Shravan40
コンパイラで警告レベルを上げてください。 char letters [];に 'error:storage size of 'の文字が分からないようなものが必要です。 –
私は正直なところ、ssに渡す文字列を使用します。文字列は '[] '演算子を使って配列のようにアクセスできます。これが何らかの学習のためのもので、 'char letters []'を 'chars [ss.size()]'に変更すれば、この問題を解決することができます。 – George