简体   繁体   English

我没有得到所需的期望输出

[英]I am not getting the desired output as required

The output of this program is to get the number of words in a sentence irrespective of number of whitespaces or any of the symbols as mentioned in the question. 该程序的输出是获取句子中单词的数量,而与空格或问题中提到的任何符号无关。

Here's the question: 这是问题:

A line of English text will be given, where words are separated by one of the following symbols: 将给出一行英文文本,其中单词由以下符号之一分隔:

' ', '\\t', '.', ',' and ';' '','\\ t','。',','和';'

Each word may be separated from the next and the previous by one or more of the following symbols. 每个单词可以由下一个或多个以下符号分隔。 You have to count the number of words in the sentence. 您必须计算句子中的单词数。

Note that to read the input, you have to read until the EOF symbol is read, as in the following example. 请注意,要读取输入,必须读取直到读取到EOF符号为止,如以下示例所示。

int main()
{
    int c;

    c = getchar();
    while ( c != EOF ) {
       c = getchar();
    }
    return 0;
}

Note: it is possible to solve this question without arrays, so the maximum length of the line is not important. 注意:无需数组即可解决此问题,因此线的最大长度并不重要。

Input 输入

A line of English text with words separated from one another by one or more occurrences of the symbols 一行英文单词,单词之间被一个或多个符号隔开

' ', '\\t', '.', ',' and ';' '','\\ t','。',','和';'

Output 产量

The number of words in the line. 该行中的单词数。

Sample Input 样本输入

This is a sentence, it has words separated by spaces and fullstops.

Sample Output 样本输出

12

I know I have to use the format given in the question, but I didn't know how to use it. 我知道我必须使用问题中给出的格式,但是我不知道如何使用它。

I only know the program to find number of words in a sentence when the words are separated by only single space or a single \\t. 我只知道当单词之间仅用单个空格或单个\\ t分隔时才能在句子中查找单词数的程序。 But, I didn't get any idea to solve this question. 但是,我不知道要解决这个问题。

[one of delimiters]...[one of delimiters][not one of delimiters]... 
                                        ^^ count this edge.

like this 像这样

#include <stdio.h>
#include <stdbool.h>

bool IsDelimiter(char ch, const char *delimiters){
    while(*delimiters)
        if(*delimiters++ == ch)
            return true;
    return false;
}

int main(void){
    bool prev_is_delmiter = true;
    int ch, wc = 0;

    while((ch = getchar()) != EOF && ch != '\n'){
        bool current_is_delmiter = IsDelimiter(ch, " \t,.;");
        if(prev_is_delmiter && !current_is_delmiter){
            ++wc;
        }
        prev_is_delmiter = current_is_delmiter;
    }
    printf("%d\n", wc);
}

This is a simple application of four elements. 这是四个元素的简单应用。

  1. while() 而()
  2. getchar() getchar函数()
  3. switch() 开关()
  4. printf() 的printf()

the following code shows one way to implement the function: 以下代码显示了实现该功能的一种方法:

#include <stdio.h>  // getchar(), EOF, printf()

int main( void )
{
    int wordCount = 0;
    int ch;
    int inWord = 0;

    while( (ch = getchar()) != EOF )
    {
        switch( ch )
        {
            case ' ':
            case '\t':
            case '.':
            case ',':
            case ';':
                inWord = 0;
                break;

            default:
                if ( !inWord )
                {
                    wordCount++;
                    inWord = 1;
                }
                break;
        } // end switch
    } // end while

    printf( "\n%d\n", wordCount );
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM