C语言:统计一个文本文件中字母,数字及其他字符各有多少个,是编写相应程序

如题所述

源代码如下:

#include<stdio.h>

#include<string.h>

void main()

{

 char str[20];

 int num=0,letter=0,other=0;

 int i=0;

scanf("%s",str);

for(i=0; i<strlen(str); i++)

{

if(str[i]>='0'&&str[i]<='9') num++;

 else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z') letter++;

else other++;

 }

printf("numbers: %d\nletters: %d\nothers: %d\n",num,letter,other);

}

扩展资料

1、统计文件的字符数、单词数以及总行数,包括每行的字符数和单词数。

2、空白字符(空格和tab缩进)不计入字符总数;单词以空格为分隔。不考虑一个单词在两行的情况,限制每行的字符数不能超过1000。

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-11-02
#include<stdafx.h>
#include<stdio.h>
#include <stdlib.h>

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);
}本回答被提问者采纳
第2个回答  2010-05-21
没分也想要程序????
给我100分替你写
相似回答