|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 kingze1992 于 2014-8-24 13:09 编辑
求助: 以下代码
- #include <stdio.h>
- int letter = 0, number = 0, space = 0, others = 0;
- int main()
- {
- void count(char str[]);
- char str[80];
- printf("Input string:\n");
- gets(str);
- printf("The string is:\n");
- puts(str);
- count(str);
- printf("\nletter:%d\tnumber:%d\tothers:%d\tspace:%d\n", letter, number, others, space);
- return 0;
- }
- void count(char str[80])
- {
- int i;
- for (i = 0; str[i] != '\0'; i++)
- {
- if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
- letter++;
- else if (str[i] >= '0' && str[i] <= '9')
- number++;
- else if (str[i] == ' ')
- space++;
- else
- others++;
- }
- }
复制代码
为什么输出是这样:
Input string:
aaa 12bbb3 !@#$%
The string is:
aaa 12bbb3 !@#$%
letter:6 number:3 space:2 others:5
请按任意键继续. . .
但是把 printf语句中的 space 和 others 换个位置,
printf("\nletter:%d\tnumber:%d\tothers:%d\tspace:%d\n", letter, number, others, space);
就正常了:
Input string:
aaa 12bbb3 !@#$%
The string is:
aaa 12bbb3 !@#$%
letter:6 number:3 others:5 space:2
请按任意键继续. . .
|
|