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