帮我改改错吧,我实在是找不出了...

作者在 2010-04-18 17:46:33 发布以下内容

题目:从键盘上输入一串字符,编程统计其中字母、空格、数字及其他字符的个数。

 

/*输出结果有时不对*/

main()

{

  int i,k=0,m=0,n=0;

  char ch[20];

  gets(ch);

  for(i=0;i<=20;i++)

    if(ch[i]>='a'&&ch[i]<='z'||ch[i]>='A'&&ch[i]<='Z')

      k++;

    else if(ch[i]==' ')

      m++;

    else if(ch[i]>='0'&&ch[i]<='9')

      n++;

  printf("k=%d,m=%d,n=%d",k,m,n);

  getch();

}

问题 | 阅读 909 次
文章评论,共12条
breezemiss
2010-04-18 18:19
1
累。头晕晕。看不出来。
红色杀戮(作者)
2010-04-18 18:40
2
呵呵,我也被它搞得头晕啊,看不出来就运行一下吧…
Spygg
2010-04-18 22:27
3
#include&lt;stdio.h&gt;<br />
int main(void)<br />
<br />
{<br />
<br />
&nbsp;&nbsp;int letter=0,space=0,num=0,other=0;<br />
<br />
&nbsp;&nbsp;char ch[200],*s;<br />
<br />
&nbsp;&nbsp;s=ch;<br />
&nbsp;&nbsp;gets(s);<br />
<br />
&nbsp;&nbsp;while(*s!='\0')<br />
&nbsp;&nbsp;{<br />
<br />
&nbsp; &nbsp; if(*s&gt;='a'&amp;&amp;*s&lt;='z'||*s&gt;='A'&amp;&amp;*s&lt;='Z')<br />
&nbsp; &nbsp;&nbsp; &nbsp;letter++;<br />
&nbsp; &nbsp; else if(*s==' ')<br />
&nbsp; &nbsp;&nbsp; &nbsp;space++;<br />
&nbsp; &nbsp; else if(*s&gt;='0'&amp;&amp;*s&lt;='9')<br />
&nbsp; &nbsp;&nbsp; &nbsp;num++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; other++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; s++;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;printf(&quot;letter=%d,space=%d,num=%d,other=%d&quot;,letter,space,num,other);<br />
&nbsp;&nbsp;getch();<br />
<br />
}
Spygg
2010-04-18 22:28
4
你自己对比下吧,就知道问题在那里了
红色杀戮(作者)
2010-04-18 23:31
5
<div class="quote"><span class="q"><b>Spygg</b>: 你自己对比下吧,就知道问题在那里了</span></div>呵呵,你用的指针啊,我们还没学呢,不过好懂,比我写的清晰多了,也发现了我的错误,谢啦…
Spygg
2010-04-19 00:15
6
<div class="quote"><span class="q"><b>红色杀戮</b>: 呵呵,你用的指针啊,我们还没学呢,不过好懂,比我写的清晰多了,也发现了我的错误,谢啦…</span></div>不用指针也行,数组是一样的
JAVATWO
2010-04-22 11:39
7
Spygg 学的停好啊 嘿嘿
月光321
2010-04-23 09:07
8
#include &lt;stdio.h&gt;<br />
#include &lt;string.h&gt;<br />
#include &lt;conio.h&gt;<br />
<br />
<br />
 void main()<br />
{<br />
<br />
&nbsp;&nbsp;int i,k=0,m=0,n=0;<br />
<br />
&nbsp;&nbsp;char ch[20];<br />
<br />
&nbsp;&nbsp;gets(ch);<br />
<br />
for(i=0;i&lt;=20;i++)<br />
&nbsp;&nbsp;if(ch<i>!=0)<br />
&nbsp;&nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if(ch<i>&gt;='a'&amp;&amp;ch<i>&lt;='z'||ch<i>&gt;='A'&amp;&amp;ch<i>&lt;='Z')<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;k++;<br />
<br />
&nbsp; &nbsp; else if(ch<i>==' ')<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;m++;<br />
<br />
&nbsp; &nbsp; else if(ch<i>&gt;='0'&amp;&amp;ch<i>&lt;='9')<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n++;<br />
&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;printf(&quot;k=%d,m=%d,n=%d&quot;,k,m,n);<br />
&nbsp; &nbsp;&nbsp; &nbsp;getch();<br />
}
longxuanxuan
2010-04-26 10:25
9
是javascript&nbsp;&nbsp;吗?上面的?
breezemiss
2010-04-26 10:52
10
<div class="quote"><span class="q"><b>longxuanxuan</b>: 是javascript&nbsp;&nbsp;吗?上面的?</span></div>是C语言。。。。
用心对待生活
2010-04-27 16:02
11
char ch[20];<br />
&nbsp;&nbsp;for(i=0;i&lt;=20;i++)<br />
&nbsp; &nbsp; if(ch<i>&gt;=有越界问题啊
xgpfree
2010-05-05 12:14
12
#include &quot;stdio.h&quot;<br />
#include &quot;string.h&quot;<br />
void main()<br />
{<br />
 char str[100],m;<br />
 int i,n,space=0,num=0,letter=0,other=0;<br />
 printf(&quot;请输入一串字符!按回车结束!\n&quot;);<br />
 gets(str);<br />
 n=strlen(str);<br />
 for(i=0;i&lt;=n-1;i++)<br />
 {<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;m=str<i>;<br />
&nbsp;&nbsp;if(m==32)space++;<br />
&nbsp;&nbsp;else if(m&gt;='A'&amp;&amp;m&lt;='Z'||m&gt;='a'&amp;&amp;m&lt;='z')letter++;<br />
&nbsp;&nbsp;else if(m&gt;='0'&amp;&amp;m&lt;='9')num++;<br />
&nbsp;&nbsp;else other++;<br />
 }<br />
 printf(&quot;有字母%d个,数字%d个,空格%d个,其他字符%d个。&quot;,letter,num,space,other);<br />
}
游客请输入验证码
浏览4553次
文章分类