c语言的问题
在其他的论坛发现一段代码,统计大写字母时说是比实际多一个,不知怎么回事,代码如下(不知为什么有些符号在这里显示不出来,下面是代码载图):#include<stdio.h>
void main()
{
char c,str;
int i,j,bw=0,lw=0,num=0,ept=0,other=0;/*bw大写字母,lw小写字母,num数字,ept空格,other其他*/
for(i=0;i<3;i++)
gets(str);
for(i=0;i<3;i++)
{ for(j=0;j<80;j++)
{
c=str;
if(c>='a' && c<='z')
lw++;
else if(c>='A' && c<='Z')
bw++;
else if(c>='0' && c<='9')
num++;
else if(c==' ')
ept++;
else other++;
}
}
printf("lw=%d,bw=%d,num=%d,ept=%d,other=%d\n",lw,bw,num,ept,other);
}
我的测试结果如下:
[ 本帖最后由 六月飞霜 于 2008-7-27 15:17 编辑 ] 类似的,有无参考价值~~?
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
2.程序源代码:
#include "stdio.h"
main()
{char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
} 原帖由 叶 于 2008-7-27 15:38 发表 https://www.gdutbbs.com/images/common/back.gif
类似的,有无参考价值~~?
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
2.程序源代码:
#include "stdio ...
现在问题是为什么其他的统计都正确,唯独大写字母的统计不正确? 你的数组都没初始化,编译器随机赋的值谁也不知道是什么,说不定在你的机器上跑刚好就给赋了个大写字母也说不定。
把for循环改为while循环,把循环条件改为判断是否\n,或者是否串结束试试看。 关键在于数组没初始化,而作者偏偏要把80个字节遍历一遍。 原帖由 gyCai 于 2008-7-27 16:35 发表 https://www.gdutbbs.com/images/common/back.gif
关键在于数组没初始化,而作者偏偏要把80个字节遍历一遍。
的确是这个原因,谢了!! 原帖由 abcd932 于 2008-8-4 20:04 发表 https://www.gdutbbs.com/images/common/back.gif
China's registered urban and township unemployment rate stood at four percent in the first half, down 0.2 percentage point from the same world of warcraft goldperiod last year, the Ministry of Human R ...
ls的你在这里发了什么鬼东西?? 你得代码加一句(红色的那一句),对于系统初始化的数据不测试,只测试那些那读入的
#include<iostream>
#include<stdio.h>
int main()
{
char c,str;
int i,j,bw=0,lw=0,num=0,ept=0,other=0;/*bw大写字母,lw小写字母,num数字,ept空格,other其他*/
for(i=0;i<3;i++)
gets(str);
for(i=0;i<3;i++)
{ for(j=0;j<80&&str;j++)
{
c=str;
if(c>='a' && c<='z')
lw++;
else if(c>='A' && c<='Z')
bw++;
else if(c>='0' && c<='9')
num++;
else if(c==' ')
ept++;
else other++;
}
}
printf("lw=%d,bw=%d,num=%d,ept=%d,other=%d\n",lw,bw,num,ept,other);
system("pause");
return 0;
}
页:
[1]