Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在我的字符串中找不到以标点符号(如引号)开头的准确字数_C++_String_Count_Words_Punctuation - Fatal编程技术网

C++ 在我的字符串中找不到以标点符号(如引号)开头的准确字数

C++ 在我的字符串中找不到以标点符号(如引号)开头的准确字数,c++,string,count,words,punctuation,C++,String,Count,Words,Punctuation,我正在编写一个代码,其中我应该查找字符串中的字数,知道每个字都可以由a-Z(或a-Z)以外的任何字符分隔。我写的代码只有在句首没有标点符号的情况下才能正常工作。但是,当用引号(即“仅连接”)等标点符号开始句子时,问题就来了。结果将显示3个单词,而不是2个单词。我用C++编程,用DEV-C++编写。谢谢你的帮助。我的代码如下: #include <cstring> #include <iostream> #include <conio.h> #include &

我正在编写一个代码,其中我应该查找字符串中的字数,知道每个字都可以由a-Z(或a-Z)以外的任何字符分隔。我写的代码只有在句首没有标点符号的情况下才能正常工作。但是,当用引号(即“仅连接”)等标点符号开始句子时,问题就来了。结果将显示3个单词,而不是2个单词。我用C++编程,用DEV-C++编写。谢谢你的帮助。我的代码如下:

#include <cstring>
#include <iostream>
#include <conio.h>
#include <ctype.h>

using namespace std;

int getline();   //scope
int all_words(char prose[]);  //scope

int main()
{
   getline();
   system ("Pause");
   return 0;
}


int getline()
{
    char prose[600];
    cout << "Enter a sentence: ";

    cin.getline (prose, 600); 
    all_words(prose);
    return 0;
}


int all_words(char prose[])
{ int y, i=0, k=0; int count_words=0; char array_words[600], c;

    do
     {

        y=prose[i++];
        if (ispunct(y))  //skeep the punctuation
         i++;

         if ((y<65 && isspace(y)) || (y<65 && ispunct(y)))     //Case where we meet spacial character or punctuation follwed by space

          count_words++;   //count words

         if (ispunct(y))  //skeep the punctuation
           i++;

    }while (y); //till we have a character

    cout <<endl<<" here is the number of words  "<< count_words <<endl;

   return 0; 

 }



 ***********************************Output******************************
  Enter a sentence: "Only connect!"

  here is the number of words  3

  Press any key to continue . . .
#包括
#包括
#包括
#包括
使用名称空间std;
int getline()//范围
整型所有单词(字符散文[])//范围
int main()
{
getline();
系统(“暂停”);
返回0;
}
int getline()
{
字符散文[600];

cout我想你需要重新考虑你的算法。在我的脑海中,我可能会这样做:

  • 循环而不是字符串的结尾
    • 跳过所有非字母字符(
      ,而(!std::isalpha)
    • 跳过所有字母字符(
      while(std::isalpha)
    • 增加字计数器
记住还要检查这些内部循环中的字符串结尾。

首先

if (ispunct(y))
在第一个引号中有递增计数器i,但变量y仍然包含“,这将产生第二个条件为true。最后一个“为计算单词++提供额外的递增”

if ((y<65 && isspace(y)) || (y<65 && ispunct(y)))

如果((回答[1]你的问题。[1]: