c语言中统计单词数目

1、

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

int main(void)
{
    char ch;
    bool word = false;
    bool space = false;
    int words = 0;
    
    while((ch = getchar()) != EOF)
    {
        if(!isspace(ch))
            word = true;
        if(isspace(ch))
            space = true;
        if(word && space)
        {
            words++;
            word = false;
            space = false;
        }
    }
    
    printf("words: %d.\n", words);
    
    return 0;
}

 

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

int main(void)
{
    int ch;
    int words = 0;
    bool inward = false;
    
    while((ch = getchar()) != EOF)
    {
        if(!isspace(ch) && !inward)
        {
            words++;
            inward = true;
        }
        if(isspace(ch) && inward)
        {
            inward = false;
        }
    }
    
    printf("words: %d.\n", words);
    
    return 0;
}

 

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

int main(void)
{
    int ch;
    bool inword = false;
    int words = 0;
    
    while((ch = getchar()) != EOF)
    {
        if(!isspace(ch) && !inword)
        {
            words++;
            inword = true;
        }
        
        if(isspace(ch) && inword)
            inword = false;
    }
    
    printf("words: %d.\n", words);
    
    return 0;
}

 

posted @ 2021-08-29 16:23  小鲨鱼2018  阅读(708)  评论(0)    收藏  举报