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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM