counting the number of sentences in a paragraph in

2019-07-16 04:14发布

As part of my course, I have to learn C using Turbo C (unfortunately).

Our teacher asked us to make a piece of code that counts the number of characters, words and sentences in a paragraph (only using printf, getch() and a while loop.. he doesn't want us to use any other commands yet). Here is the code I wrote:

#include <stdio.h>
#include <conio.h>

void main(void)  
{  
clrscr();  
int count = 0;  
int words = 0;  
int sentences = 0;  
char ch;

while ((ch = getch()) != '\n')
{
    printf("%c", ch);
    while ((ch = getch()) != '.')
    {
        printf("%c", ch);
        while ((ch = getch()) != ' ')
        {
            printf("%c", ch);
            count++;
        }
        printf("%c", ch);
        words++;
    }
    sentences++;
}

printf("The number of characters are %d", count);
printf("\nThe number of words are %d", words);
printf("\nThe number of sentences are %d", sentences);
getch();
   }

It does work (counts the number of characters and words at least). However when I compile the code and check it out on the console window I can't get the program to stop running. It is supposed to end as soon as I input the enter key. Why is that?

7条回答
老娘就宠你
2楼-- · 2019-07-16 04:49

I think the problem is because of your outer while loop's condition. It checks for a newline character '\n', as soon as it finds one the loop terminates. You can try to include your code in a while loop with the following condition

while((c=getchar())!=EOF)

this will stop taking input when the user presses Ctrl+z

Hope this helps..

查看更多
放我归山
3楼-- · 2019-07-16 04:59
int ch;
int flag;
while ((ch = getch()) != '\r'){
    ++count;
    flag = 1;
    while(flag && (ch == ' ' || ch == '.')){
        ++words;//no good E.g Contiguous space, Space at the beginning of the sentence
        flag = 0;;
    }
    flag = 1;
    while(flag && ch == '.'){
        ++sentences;
        flag=0;
    }
    printf("%c", ch);
}
printf("\n");
查看更多
仙女界的扛把子
4楼-- · 2019-07-16 05:00
#include <stdio.h>
#include <ctype.h>

int main(void){

int sentence=0,characters =0,words =0,c=0,inside_word = 0,temp =0;
// while ((c = getchar()) != EOF) 
while ((c = getchar()) != '\n') {
   //a word is complete when we arrive at a space after we 
  // are inside a word or when we reach a  full stop

    while(c == '.'){
        sentence++;
        temp = c;
        c = 0;
    }
     while (isalnum(c)) {
        inside_word = 1;
        characters++;
        c =0;
    }
    while ((isspace(c) || temp == '.') && inside_word == 1){
        words++;
        inside_word = 0;
        temp = 0;
        c =0;
    }
}
printf(" %d   %d   %d",characters,words,sentence);
return 0;
}

this should do it,

isalnum checks if the letter is alphanumeric, if its an alphabetical letter or a number, I dont expect random ascii characters in my sentences in this program.

isspace as the name says check for space

you need the ctype.h header for this. or you could add in

   while(c == ' ') and whie((c>='a' && c<='z') || (c >= 'A' && c<='Z') 

if you don't want to use isalpace and isalnum, your choice, but it will be less elegant :)

查看更多
【Aperson】
5楼-- · 2019-07-16 05:06

The trouble with your code is that you consume the characters in each of your loops. a '\n' will be consumed either by the loop that scans for words of for sentences, so the outer loop will never see it.

Here is a possible solution to your problem:

int sentences = 0;
int words = 0;
int characters = 0;

int in_word = 0; // state of our parser

int ch;
do
{
    int end_word = 1; // consider a word wil end by default
    ch = getch();
    characters++; // count characters
    switch (ch)
    {
    case '.':
        sentences++; // any dot is considered end of a sentence and a word
        break;
    case ' ': // a space is the end of a word
        break;
    default:
       in_word = 1;  // any non-space non-dot char is considered part of a word
       end_word = 0; // cancel word ending
    }

    // handle word termination
    if (in_word and end_word) 
    {
        in_word = 0;
        words++;
    }

} while (ch != '\n');

A general approach to these parsing problems is to write a finite-state machine that will read one character at a time and react to all the possible transitions this character can trigger.

In this example, the machine has to remember if it is currently parsing a word, so that one new word is counted only the first time a terminating space or dot is encountered.

This piece of code uses a switch for concision. You can replace it with an if...else if sequence to please your teacher :).

If your teacher forced you to use only while loops, then your teacher has done a stupid thing. The equivalent code without other conditional expressions will be heavier, less understandable and redundant.

Since some people seem to think it's important, here is one possible solution:

int sentences = 0;
int words = 0;
int characters = 0;

int in_word = 0; // state of our parser
int ch;

// read initial character
ch = getch();

// do it with only while loops
while (ch != '\n')
{
    // count characters
    characters++;

    // count words
    while (in_word)
    {
        in_word = 0;
        words++;
    }

    // skip spaces
    while (ch == ' ')
    {
        ch = -1;
    }

    // detect sentences
    while (ch == '.')
    {
        sentences++;
        ch = -1;
    }

    // detect words
    while ((ch != '\n')
    {
        word_detected = 1;
        ch = -1;
    }

    // read next character
    ch = getch();
}

Basically you can replace if (c== xxx) ... with while (c== xxx) { c = -1; ... }, which is an artifical, contrieved way of programming.

An exercise should not promote stupid ways of doing things, IMHO.
That's why I suspect you misunderstood what the teacher asked.
Obviously if you can use while loops you can also use if statements.

Trying to do this exercise with only while loops is futile and results in something that as little or nothing to do with real parser code.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-07-16 05:10

Here you have the solution to your problem:

#include <stdio.h>
#include <conio.h>

void main(void)
{
    clrscr();  
    int count = 0;  
    int words = 0;  
    int sentences = 0;  
    char ch;

    ch = getch();
    while (ch != '\n')
    {
        while (ch != '.' && ch != '\n')
        {
            while (ch != ' ' && ch != '\n' && ch != '.')
            {
                count++;
                ch = getch();
                printf("%c", ch);
            }
            words++;
            while(ch == ' ') {
                ch = getch();
                printf("%c", ch);
            }
        }
        sentences++;
        while(ch == '.' && ch == ' ') {
           ch = getch();
           printf("%c", ch);
        }
    }

    printf("The number of characters are %d", count);
    printf("\nThe number of words are %d", words);
    printf("\nThe number of sentences are %d", sentences);
    getch();
}

The problem with your code is that the innermost while loop was consuming all the characters. Whenever you enter there and you type a dot or a newline it stays inside that loop because ch is different from a blank. However, when you exit from the innermost loop you risk to remain stuck at the second loop because ch will be a blank and so always different from '.' and '\n'. Since in my solution you only acquire a character in the innermost loop, in the other loops you need to "eat" the blank and the dot in order to go on with the other characters.

Checking these conditions in the two inner loops makes the code work. Notice that I removed some of your prints.

Hope it helps.

Edit: I added the instructions to print what you type and a last check in the while loop after sentences++ to check the blank, otherwise it will count one word more.

查看更多
劫难
7楼-- · 2019-07-16 05:14

You can implement with ease an if statement using while statement:

bool flag = true;
while(IF_COND && flag)
{
    //DO SOMETHING
    flag = false;
}

just plug it in a simple solution that uses if statements.

For example:

#include <stdio.h>
#include <conio.h>

void main(void)  
{  
    int count = 0;  
    int words = 1;  
    int sentences = 1;  
    char ch;

    bool if_flag;

    while ((ch = getch()) != '\n')
    {
        count++;
        if_flag = true;
        while (ch==' ' && if_flag)
        {
            words++;
            if_flag = false;
        }
        if_flag = true;
        while (ch=='.' && if_flag)
        {
            sentences++;
            if_flag = false;
        }
    }

    printf("The number of characters are %d", count);
    printf("\nThe number of words are %d", words);
    printf("\nThe number of sentences are %d", sentences);
    getch();
}
查看更多
登录 后发表回答