C++ C++;一次打印一个字的字符串,计算字符数并平均字符数

C++ C++;一次打印一个字的字符串,计算字符数并平均字符数,c++,string,C++,String,我如何从每行的字符串中打印一个单词,并将其旁边的字符数和平均字符数一起打印出来?我假设使用字符串成员函数将对象转换为c字符串。函数countWords接受c字符串并返回一个int。假定函数读取每个单词及其长度,包括字符的平均值。我已经做了多少字是在字符串中,除了我不知道如何继续其余 例如:超级伟大的加农炮男孩 超级5 伟大的5 加农炮6 男孩4 平均字符数:5 这是我目前的计划: #include <iostream> #include <string> #include

我如何从每行的字符串中打印一个单词,并将其旁边的字符数和平均字符数一起打印出来?我假设使用字符串成员函数将对象转换为c字符串。函数countWords接受c字符串并返回一个int。假定函数读取每个单词及其长度,包括字符的平均值。我已经做了多少字是在字符串中,除了我不知道如何继续其余

例如:超级伟大的加农炮男孩

超级5

伟大的5

加农炮6

男孩4

平均字符数:5

这是我目前的计划:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int countWords(char *sentence);

int main()
{
    const int size=80;
    char word[size];
    double average=0;
    cout<<"Enter words less than " <<size-1<<" characters."<<endl;
    cin.getline(word, size);
    cout <<"There are "<<countWords(word)<<" words in the sentence."<<endl;

    return 0;
}

int countWords(char *sentence)
{
    int words= 1;
    while(*sentence != '\0')
    {
        if(*sentence == ' ')
            words++;
        sentence++;
    }
    return words;
}
#包括
#包括
#包括
使用名称空间std;
int countWords(字符*句子);
int main()
{
常数int size=80;
字符字[大小];
双平均=0;

cout你可以从这里得到启发。基本上使用
std::getline
std::cin
读取到
std::string

#包括
#包括
#包括
内联无效printWordInfo(标准::字符串和单词){

std::cout除非这类似于禁止这样做的作业,否则您几乎肯定希望使用
std::string
以及
std::getline
版本,该版本使用
std::string
而不是char:

std::string s;
std::getline(std::cin, s);
然后,您可以通过将行填充到
std::istringstream
中并从中读取单词来计算单词数:

std::istringstream buffer(s);
auto word_count = std::count(std::istream_iterator<std::string>(s), 
                             std::istream_iterator<std::string());

按照您已经拥有的内容:

您可以定义countCharacters函数,如countWords:

int countCharacters(char *sentence)
{
  int i;
  char word[size];
  for(i = 0; sentence[i] != ' '; i++) //iterate via index
  {
    word[i] = sentence[i];   //save the current word
    i++;
  }
  cout <<word<< <<i<<endl; //print word & number of chars
  return i;
}

这应该离您的需求不远了——我只对您当前的代码做了最小的修改

限制:

  • 你最好用

    string line;
    getline(cin, line);
    
    阅读该行,以便能够接受任何大小的行

  • 我现在的代码假设

    • 行首或行尾无空格
    • 两个单词之间只有一个空格
    它应该改进以适应额外的空间,但我把它留给您作为练习:-)

守则:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int countWords(char *sentence, double& average);

int main()
{
const int size=80;
char word[size];
double average=0;
cout<<"Enter words less than " <<size-1<<" characters."<<endl;
cin.getline(word, size);
cout <<"There are "<<countWords(word, average)<<" words in the sentence."<<endl;
cout << "Average of the sentence " << average << endl;
return 0;
}

int countWords(char *sentence, double& average)
{
int words= 1;
int wordlen;
char *word = NULL;
while(*sentence != '\0')
{
    if(*sentence == ' ') {
        words++;
        wordlen = sentence - word;
        average += wordlen;
        *sentence = '\0';
        cout << word << " " << wordlen<< endl;  
        word = NULL;
    }
    else if (word == NULL) word = sentence;
    sentence++;
}
wordlen = sentence - word;
average += wordlen;
cout << word << " " << wordlen<< endl;  
average /= words;
return words;

}

使用索引和
for
循环迭代字符串可能比指针算法更整洁、更容易使用。@Carcigenicate使用
std::string
并通过
为每个循环迭代字符串更容易。@NO_NAME我想他说他需要使用c字符串。@Carcigenicate抱歉。
\include
你应该测试ISSACE(*句子),也可能需要考虑标点符号使用函数ISTITT(),两个都有cType的原型。
string line;
getline(cin, line);
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int countWords(char *sentence, double& average);

int main()
{
const int size=80;
char word[size];
double average=0;
cout<<"Enter words less than " <<size-1<<" characters."<<endl;
cin.getline(word, size);
cout <<"There are "<<countWords(word, average)<<" words in the sentence."<<endl;
cout << "Average of the sentence " << average << endl;
return 0;
}

int countWords(char *sentence, double& average)
{
int words= 1;
int wordlen;
char *word = NULL;
while(*sentence != '\0')
{
    if(*sentence == ' ') {
        words++;
        wordlen = sentence - word;
        average += wordlen;
        *sentence = '\0';
        cout << word << " " << wordlen<< endl;  
        word = NULL;
    }
    else if (word == NULL) word = sentence;
    sentence++;
}
wordlen = sentence - word;
average += wordlen;
cout << word << " " << wordlen<< endl;  
average /= words;
return words;

}
Enter words less than 79 characters.
super great cannon boys
super 5
great 5
cannon 6
boys 4
There are 4 words in the sentence.
Average of the sentence 5