Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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_Algorithm - Fatal编程技术网

用C计算单词的最好方法是什么?

用C计算单词的最好方法是什么?,c,algorithm,C,Algorithm,这是一个简单的计算单词的程序,我觉得非常有效。这是C语言中计算单词的最好方法,还是这个程序有任何缺陷 #include <stdio.h> int CountWords(void); main() { printf("count the words and enter string\n"); CountWords(); } int

这是一个简单的计算单词的程序,我觉得非常有效。这是C语言中计算单词的最好方法,还是这个程序有任何缺陷

         #include <stdio.h>
         int CountWords(void);
         main()
         {
            printf("count the words and enter string\n");
            CountWords();

         }
        int CountWords(void)
        {
            char c;
            int num=0;
            int flag= 0;
             while((c=getchar())!='\n')
            {
              if(c==' ')
              {
                flag=0 ;        
              }                         
              else if(flag==0)
              {
                num++;
                flag=1;     
              }                      
           }
              printf("Num is %d",num);
         }     
#包括
int CountWords(void);
main()
{
printf(“计算字数并输入字符串”);
CountWords();
}
int CountWords(void)
{
字符c;
int num=0;
int标志=0;
而((c=getchar())!='\n')
{
如果(c='')
{
flag=0;
}                         
else if(标志==0)
{
num++;
flag=1;
}                      
}
printf(“Num是%d”,Num);
}     

使用此程序,您的计数将减少1。这是因为您正在检查一个空格,以便在字数中添加一个空格。您认为字符串中的最后一个单词会发生什么情况?

使用此程序,您的计数将减少1。这是因为您正在检查一个空格,以便在字数中添加一个空格。你认为字符串中的最后一个单词会发生什么情况?

初始化
flag=1
和off-by-one错误应该消失。

初始化
flag=1
和off-by-one错误应该消失。

我能想到的一件事是,如果标点符号被空格包围,它会将它们计算为单词

例如

将被报告为包含7个单词,尽管只有5个。

您可能只想将单词字符集简化为字母数字字符,并将标点符号作为单词分隔符进行计数,除非它是单词“A”或“A”,这取决于单词定义为什么(是否是单词和快速单词?每个单词有两个单词?).

我能想到的一件事是,如果标点符号被空格包围,它会将标点符号算作单词

例如

将被报告为包含7个单词,尽管只有5个。

您可能只想将单词字符集简化为字母数字字符,并将标点符号计算为单词分隔符,除非它是单词“A”或“A”,这取决于您定义的单词(它是否是快速的单词?每个单词有两个单词)。

< p>首先用算法定义问题。例如,制作流程图。像其他海报一样,在你的逻辑中运行一些快速的例子。在编写代码之前,请执行所有这些操作

然后,一旦你认为你有最好的算法,就用C写

列出一系列要问自己的问题,比如“什么是一个词?”,“什么不是一个词?”

对于解析文本或任何类型的标记,您可能会感兴趣地表达您的想法。看看任何计算机语言规范,注意它们是如何定义标识符之类的东西的。即使您不使用它来编写程序,它也会帮助您思考问题

单词是否仅限于字母a-z和a-z?连字符的词呢

可能(不是正式的BNF):

连字符:='-'
阿尔法:='a'|'b'|……|'z'|'A'|'B'|……|'Z'
alphagroup:=alpha | alpha alphagroup
连字符组:=字母组连字符组|字母组连字符组连字符组

word:=alphagroup | hyphenword

首先根据算法定义您的问题。例如,制作流程图。像其他海报一样,在你的逻辑中运行一些快速的例子。在编写代码之前,请执行所有这些操作

然后,一旦你认为你有最好的算法,就用C写

列出一系列要问自己的问题,比如“什么是一个词?”,“什么不是一个词?”

对于解析文本或任何类型的标记,您可能会感兴趣地表达您的想法。看看任何计算机语言规范,注意它们是如何定义标识符之类的东西的。即使您不使用它来编写程序,它也会帮助您思考问题

单词是否仅限于字母a-z和a-z?连字符的词呢

可能(不是正式的BNF):

连字符:='-'
阿尔法:='a'|'b'|……|'z'|'A'|'B'|……|'Z'
alphagroup:=alpha | alpha alphagroup
连字符组:=字母组连字符组|字母组连字符组连字符组

word:=alphagroup | hyphenword

下面是一段完整的代码,可以改进该程序,并且可以在所有条件下工作:

  #include <stdio.h>
         void CountWords(void);
         void main()
         {
            printf("\n\tcount the words and enter string\n\n\n");
            CountWords();

         }
        void CountWords(void)
        {
            char c;
            int num=0;
            int flag= 0;
             while((c=getchar())!='\n')
            {
            if((c==' ')||(c=='  ')||(c=='.')||(c==';')||(c==',')||(c==';')
                ||(c==':')||(c=='"')||(c=='?')||(c=='!')||(c=='-'))
              {
                flag=0 ;        
              }                         
              else if(flag==0)
              {
                num++;
                flag=1;     
              }

           }
              printf("\t\n\n\nNumber of words is %d\n",num);
         }

/*By Md. azaz*/
#包括
void CountWords(void);
void main()
{
printf(“\n\t计算单词并输入字符串\n\n\n”);
CountWords();
}
void CountWords(void)
{
字符c;
int num=0;
int标志=0;
而((c=getchar())!='\n')
{
如果((c='')| |(c='')| |(c==')| |(c==')|(c==')|(c==')|(c==')
||(c==':')| |(c=='”)| |(c=='?')| |(c=='!')| |(c=='-))
{
flag=0;
}                         
else if(标志==0)
{
num++;
flag=1;
}
}
printf(“\t\n\n\n字数为%d\n”,num);
}
/*阿扎兹博士*/

下面是一段完整的代码,可以改进该程序,并且可以在所有条件下工作:

  #include <stdio.h>
         void CountWords(void);
         void main()
         {
            printf("\n\tcount the words and enter string\n\n\n");
            CountWords();

         }
        void CountWords(void)
        {
            char c;
            int num=0;
            int flag= 0;
             while((c=getchar())!='\n')
            {
            if((c==' ')||(c=='  ')||(c=='.')||(c==';')||(c==',')||(c==';')
                ||(c==':')||(c=='"')||(c=='?')||(c=='!')||(c=='-'))
              {
                flag=0 ;        
              }                         
              else if(flag==0)
              {
                num++;
                flag=1;     
              }

           }
              printf("\t\n\n\nNumber of words is %d\n",num);
         }

/*By Md. azaz*/
#包括
void CountWords(void);
void main()
{
printf(“\n\t计算单词并输入字符串\n\n\n”);
CountWords();
}
void CountWords(void)
{
字符c;
int num=0;
int标志=0;
而((c=getchar())!='\n')
{
如果((c='')| |(c='')| |(c==')| |(c==')|(c==')|(c==')|(c==')
int numwords(char *str)
{   int n = 0;
    for(str=strtok(str, " -.!,;"); str; str=strtok(NULL, " -.!,;"))
        n++;
    return n;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define SIZE 100

int main()
{
 char str[SIZE]={ '\0' };
 int string_size;
 int wordcount, found=0, other=0;

 printf("Enter the string:");
 fgets(str, SIZE, stdin);

 string_size = strlen(str);

 for(int i=0;i<=string_size;i++)
 {
  if(isalnum(str[i]) && !isalnum(str[i-1]))
    found++;
 }

 printf("Number of words are:%d\n", found);
 return 0;
}