|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
输入一个字符串,统计其中大写字母和小写字母的个数。
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void main()
{
char a[80]; //存放字符
char str;
int i;
int b[2] = { 0 };
printf("请输入字符串:\n");
scanf("%c", &a[80]);
gets(a);
for (i = 0; (str = a[i]) != 0; i++)
{
if (str >= 'a' && str <= 'z')
b[0]=b[0]+1;
else if (str >= 'A' && str <= 'Z')
b[1]=b[1]+1;
}
printf("小写字母个数为%d", b[0]);
printf("大写字母个数为%d", b[1]);
}
本帖最后由 风过无痕1989 于 2020-11-29 17:07 编辑
你的程序没有逻辑问题,只是你要输入一个字符串,你得用循环呀,单独一条 scanf() 语句是完成不了输入的
完成了输入,这个程序就正确了。我用的是 VS2015 ,gets() 这个函数已经被废弃了,在它那里是错误,程序你自己完成就好了
我还是将我的程序给你吧:
- #define _CRT_SECURE_NO_WARNINGS 1
- #include <stdio.h>
- void main()
- {
- char a[80]; //存放字符
- char str;
- int i = 0;
- int b[2] = { 0 };
- printf("请输入字符串:\n");
-
- while ((a[i] = getchar()) != '\n')
- {
- i++;
- }
- a[i] = '0'; // 下面是最后一个单元为0作为结束符的
- i = 0;
- while((str = a[i]) != '0')
- {
- if (str >= 'a' && str <= 'z')
- b[0] = b[0] + 1;
- else if (str >= 'A' && str <= 'Z')
- b[1] = b[1] + 1;
- i++;
- }
- printf("小写字母个数为%d\n", b[0]);
- printf("大写字母个数为%d\n", b[1]);
- }
复制代码
|
-
|