C++ 我如何计算字数?

C++ 我如何计算字数?,c++,C++,如何使此代码针对计算单词的单词计数运行。每当我把一堆。。。。或它给了我13+字计数,这是不正确的 当我使用tab或“…”时,它会给我大量的数字,或者不包括它们。单词还包括(,;:!?)组 字符串字; int元音(0)、contally(0)、uppertally(0)、lowertally(0)、pointtally(0)、wordtally(1); int d; cout如果像下面这样实现,那么单词计数可能会起作用 for (int i = 1; i < d; i++) { if (

如何使此代码针对计算单词的单词计数运行。每当我把一堆。。。。或它给了我13+字计数,这是不正确的

当我使用tab或“…”时,它会给我大量的数字,或者不包括它们。单词还包括(,;:!?)组

字符串字;
int元音(0)、contally(0)、uppertally(0)、lowertally(0)、pointtally(0)、wordtally(1);
int d;

cout如果像下面这样实现,那么单词计数可能会起作用

for (int i = 1; i < d; i++) {
  if (isalpha(word[i - 1]) && !isalpha(word[i])) {
    wordtally++;
  }
}
if (d > 0 && isalpha(word[d - 1])) {
    wordtally++;
}
for(int i=1;i0&&isalpha(单词[d-1])){
wordtally++;
}

如果在“B”和“Z”之间使用间隔比较更好,则在第一个条件中不要使用大量比较。
必须使用strhr
函数,而不是在其他条件下使用许多比较。如果您重新编写代码,它将正常工作。
    //if (((word[i]!=' ')||(word[i]!='\t'))&&((word[i+1]==' ')||(word[i+1]=='\t'))) { //check work on this
}
for (int i=0; i<d; i++) {
    if (((word[i]!=' ')||(word[i]!='\t'))&&(word[i+1]==' ')){
    wordtally++;
    }
}


cout << "The number of vowels is: " << voweltally << endl;
cout << "The number of consonants is: " << contally << endl;
cout << "The number of uppercase letters is: " << uppertally << endl;
cout << "The number of lowercase letters is: " << lowertally << endl;
cout << "The number of punctuation characters is: " << punctally << endl;
cout << "The number of words is: " << wordtally << endl;
cout << "Exiting program ..." << endl;

return 0;
}
for (int i = 1; i < d; i++) {
  if (isalpha(word[i - 1]) && !isalpha(word[i])) {
    wordtally++;
  }
}
if (d > 0 && isalpha(word[d - 1])) {
    wordtally++;
}