C语言:统计一个文本文件中字母,数字及其他字符各有多少个,是编写相应程序
答案:2 悬赏:0
解决时间 2021-04-28 12:04
- 提问者网友:年齡太小℡蘿莉
- 2021-04-27 11:11
C语言:统计一个文本文件中字母,数字及其他字符各有多少个,是编写相应程序
最佳答案
- 二级知识专家网友:青春如此荒謬
- 2021-04-27 12:29
#include
#include
#include
void main()
{
char ch;
int num1=0,num2=0,num3=0;
FILE *fp;
int i;
if((fp=fopen("c:\\cpp-home.txt","r"))==NULL)
{
printf("not open");
exit(0);
}
while ((ch=fgetc(fp))!=EOF)
{
if(ch>='a'&&ch<='z')
num1++;
else if(ch>='A'&&ch<='Z')
num1++;
else if(ch>='0'&&ch<='9')
num2++;
else
num3++;
}
printf("字母:%d\n",num1);
printf("数字:%d\n",num2);
printf("其它:%d\n",num3);
fclose(fp);
}
#include
#include
void main()
{
char ch;
int num1=0,num2=0,num3=0;
FILE *fp;
int i;
if((fp=fopen("c:\\cpp-home.txt","r"))==NULL)
{
printf("not open");
exit(0);
}
while ((ch=fgetc(fp))!=EOF)
{
if(ch>='a'&&ch<='z')
num1++;
else if(ch>='A'&&ch<='Z')
num1++;
else if(ch>='0'&&ch<='9')
num2++;
else
num3++;
}
printf("字母:%d\n",num1);
printf("数字:%d\n",num2);
printf("其它:%d\n",num3);
fclose(fp);
}
全部回答
- 1楼网友:时光挺欠揍
- 2021-04-27 13:58
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "string.h"
main()
{char a[]="abcd hgh s1&&&",ch;
int i=0,j=0,k=0,l=0;
file *fp;
fp=fopen("d:\\liangnv.txt","w");
fputs(a,fp);
fclose(fp);
fp=fopen("d:\\liangnv.txt","r");
ch=fgetc(fp);
while(ch!=eof)
{ if(ch>='a'&&ch<='z')i++;
else if(ch>='a'&&ch<='z')j++;
else if(ch>='1'&&ch<='9')k++;
else l++;
ch=fgetc(fp);}
fclose(fp);
printf("小写字母%d\n",i);
printf("大写字母%d\n",j);
printf("数字\n%d\n",k);
printf("字符%d\n",l);
getch();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息