首页 > 编程语言 > 详细

c语言统计文件单词数

时间:2021-08-30 09:08:38      阅读:9      评论:0      收藏:0      [点我收藏+]

 

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语言统计文件单词数

原文:https://www.cnblogs.com/liujiaxin2018/p/15202944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!