wordcount程序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define IN 1
#define OUT 0
void main() {
FILE *fp;
int length;
fp = fopen("wang.txt","r");//打开要计数的文件
if (fp == NULL)
{
printf("can not open file");
exit(0);
}
fseek(fp, 00, SEEK_END);
length = ftell(fp);
printf("%d\n", length);//确定所要查找的文件中总字符数长度,并在屏幕中显示出来
rewind(fp);
char str[100000], c;
fread(str, sizeof(char), length, fp);//从文件中读取所有的字符到str序列
int i, num1 = 0, num2 = 0, num3, num4=0, word = OUT;
for (i = 0; (c = str[i]) != '\0'; i++)
{
if (c == ' ')//判断字符中单词数
{
num2++;
word = OUT;
}
else
{
if (word == OUT)
{
word = IN;
num1++;
}
}
if ((c = str[i]) == '\n')
{
num4++;
}
}
num3 = length - num2-num4*2;//字符数
num4++;//行数
fclose(fp);
printf("空格数为%d\n", num2);
printf("单词数为%d\n行数为%d\n字符数为%d\n", num1, num4, num3);
system("pause");

}

 

from  http://www.cnblogs.com/sunbuqiao/p/5312227.html

转载于:https://www.cnblogs.com/blueland/p/7598506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值