come in come in

villastoner 2002-05-06 08:30:39
C语言:输入一行字符,分别统计出其中英文字母、空格、数字、和其他字符的个数。
要求:尽量少用或不用<ctype.h>等头文件包涵的函数。
////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <ctype.h>

void main()
{
char c;
int A_num=0,a_num=0,space=0,num=0,others=0;

while((c=getchar())!='\n')
{
if (isupper(c)) A_num++;
else if (islower(c)) a_num++;
else if (isdigit(c)) num++;
else if (isspace(c)) space++;
else others++;
}
printf("\这一行文字中:\n大写字母有%d个",A_num);
printf("\n小写字母有%d个",a_num);
printf("\n空格有%d个",space);
printf("\n数字有%d个",num);
printf("\n其它字符有%d个\n",others);
}
/////////////////////////////////////////////////////////////////
以上的代码使用了isupper,islower,isdigit,isspace四个函数。我的意思是避免使用诸如此类的函数。
...全文
54 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
晨星 2002-05-07
  • 打赏
  • 举报
回复
算了,再贴一遍吧。

#include <stdio.h>
#include <conio.h>

void main()
{
char c;
int iCharCount = 0;
int iFigureCount = 0;
int iSpaceCount = 0;
int iOtherCount = 0;

while((c = getchar()) != 0x0a)
{
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
iCharCount++;
else if(c >= '0' && c <= '9')
iFigureCount++;
else if(c == ' ')
iSpaceCount++;
else iOtherCount++;
}

printf("There are %d charectors, %d figures, %d spaces and %d other symbols.\n" , iCharCount , iFigureCount , iSpaceCount , iOtherCount);
printf("Press any key to exit...");
getch();
}
晨星 2002-05-07
  • 打赏
  • 举报
回复
我不是给你写了一个不用的吗?

http://www.csdn.net/expert/topic/699/699372.xml?temp=.4252283
villastoner 2002-05-07
  • 打赏
  • 举报
回复
up
blue_soft 2002-05-06
  • 打赏
  • 举报
回复
好象是程序考试中的一道题目,以前做到过。
寂寞漂泊 2002-05-06
  • 打赏
  • 举报
回复
为什么??????
morningsing 2002-05-06
  • 打赏
  • 举报
回复
为什么??????
snipersu 2002-05-06
  • 打赏
  • 举报
回复
用ASCII码的值来判断。
蚊子王 2002-05-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <ctype.h>

void main()
{
char c;
int A_num=0,a_num=0,space=0,num=0,others=0;

while((c=getchar())!='\n')
{
if (c>='A'&&c<='Z')) A_num++;
else if (c>='a'&&c<='z')) a_num++;
else if (c>='0'&&c<='9')) num++;
else if (c==' '||c=='\t')) space++;
else others++;
}
printf("\这一行文字中:\n大写字母有%d个",A_num);
printf("\n小写字母有%d个",a_num);
printf("\n空格有%d个",space);
printf("\n数字有%d个",num);
printf("\n其它字符有%d个\n",others);
}
fredwu 2002-05-06
  • 打赏
  • 举报
回复
ASCII:大写字母0x41-0x5A,小写为0x60-0x7A,数字0x30-0x39,空格0x20,
一切都可以进行了吧?

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧