【问题描述】已经存在一个文本文件1.txt,其中存放了一些字符信息,统计文件中包含的句子个数,以及字符个数。将信息写入文件2.txt中。
【输入形式】从文件1.txt中读入信息。
【输出形式】把统计好的信息输出到2.txt文件中。
【样例输入】
Whatever is worth doing is worth doing well. Happiness is a way station between too much and too little. In love folly is always sweet.
【样例输出】
3 135
句子是只用.分隔吗
代码:
#include <stdio.h>
int main()
{
char ch;
FILE* fp = fopen("1.txt", "r");
FILE* fp2 = fopen("2.txt", "w");
int jz = 0, cn = 0; //句子和字符数量
if (fp == 0)
return 0;
else
{
while (!feof(fp))
{
if (fread(&ch, 1, 1, fp))
{
cn++; //字符个数++
if (ch == '.')
jz++;
}
}
fclose(fp);
fprintf(fp2, "%d %d", jz, cn);
fclose(fp2);
}
return 0;
}
句子是根据句号判断呗?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("1.txt","r");
char buf[10001];
fread(buf,1,10001,fp);
fclose(fp);
int count = 0,num = 0;
while(buf[count] != '\0')
{
if(buf[count] == '.')
num++;
count++;
}
fp = fopen("2.txt","w");
fprintf(fp,"%d %d",num,count);
fclose(fp);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!