编写代码实现以下功能:使用 gets()函数获取用户由键盘任意输入的一段
文字(英文输入法状态),给出其中包含的小写字母数量、大写字母数量、空格数量、其他字符数量。
用gets
#include <iostream>
using namespace std;
int main()
{
char s[1000];
gets(s);
int a=0,b=0,c=0,d=0,i=0;
while(s[i] != 0)
{
if(s[i]>='a' && s[i] <='z')
a++;
else if(s[i]>='A' && s[i] <='Z')
b++;
else if(s[i] == ' ')
c++;
else
d++;
i++;
}
printf("大写字符:%d,小写字符:%d,空格字符:%d,其它字符:%d\n",a,b,c,d);
}
用C给你写,C++你可以去网站上搜搜,都会有的!有用的话,给个关注,写得不易!谢谢了!
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
int lowercase = 0, uppercase = 0, space = 0, other = 0;
printf("Please enter a string:\n");
while ((c = getchar()) != '\n') {
if (islower(c)) {
lowercase++;
} else if (isupper(c)) {
uppercase++;
} else if (c == ' ') {
space++;
} else {
other++;
}
}
printf("Lowercase letters: %d\n", lowercase);
printf("Uppercase letters: %d\n", uppercase);
printf("Spaces: %d\n", space);
printf("Other characters: %d\n", other);
return 0;
}
该程序首先使用 getchar() 函数逐个读取用户输入的字符,直到读取到换行符为止。然后使用 islower()、isupper() 和判断字符是否为空格,从而统计小写字母数量、大写字母数量和空格数量,剩余的字符则归为其他字符数量。最后将统计结果输出即可。
不知道你这个问题是否已经解决, 如果还没有解决的话: