计算那是c中的两个单词

时间:2014-10-26 00:16:13

标签: c counting words apostrophe

#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>

int main()
{
    unsigned long c;
    unsigned long line;
    unsigned long word;
    char ch;

    c = 0;
    line = 0;
    word = 0;

    printf("Please enter text:\n");
    while((ch = getchar()) != EOF)
    {
        c ++;
        if (ch == '\n')
        {
        line ++;
        }
        if (ch == ' ' || ch == '\n')
        {
        word ++;
        }
    }
    printf( "%lu %lu %lu\n", c, word, line );
    return 0;
}

现在我的程序正常工作,它正确计算字符,单词和行。但对于像那样的单词,程序将其计为1个单词,我希望它计为2个单词。我需要添加什么才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

if(ch =='\'' || ch == ' ' || ch == '\n')
{
    word++;
}