c语言统计文件单词数

 

1、

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

int main(void)
{
    int ch;
    char filename[128];
    
    FILE * fp;
    bool word = false;
    bool space = false;
    int words = 0;
    
    printf("please input the filename: ");
    scanf("%s", filename);
    
    fp = fopen(filename, "r");
    
    if(fp == NULL)
    {
        printf("file open failed.\n");
        exit(1);    
    }    
    
    while((ch = fgetc(fp)) != EOF)
    {
        putchar(ch);
        if(!isspace(ch))
            word = true;
        if(isspace(ch))
            space = true;
        if(word && space)
        {
            words++;
            word = false;
            space = false;
        }
        if(!word && space)
        {
            space = false;    
        } 
    }
    if(word)
        words++;
    printf("\nwords: %d.\n", words);
        
    return 0;
} 

c语言统计文件单词数

 

c语言统计文件单词数

上一篇:ios手机竖屏拍照后上传,图片显示时会90度翻转


下一篇:java根据年月日序号生成主键