用C语言输入一串字符,以问号(?)为结束标志,分别统计输出小写字符的个数和数字的个数
时间: 2024-04-07 21:30:12 浏览: 22
好的,下面是用C语言输入一串字符,并统计输出小写字符和数字个数的代码:
```c
#include <stdio.h>
int main() {
char input;
int lowercaseCount = 0;
int digitCount = 0;
printf("请输入一串字符,以问号(?)为结束标志:\n");
while (1) {
scanf("%c", &input);
if (input == '?') {
break;
}
if (input >= 'a' && input <= 'z') {
lowercaseCount++;
}
if (input >= '0' && input <= '9') {
digitCount++;
}
}
printf("小写字符个数:%d\n", lowercaseCount);
printf("数字个数:%d\n", digitCount);
return 0;
}
```
你可以运行这段代码,输入一串字符,以问号(?)作为结束标志。然后代码会统计并输出小写字符的个数和数字的个数。注意,这里假设输入的字符只包含小写字母和数字。
相关问题
C语言输入一串字符,以字符‘#’作为结束标志,分别统计其中大写字母、小写字母、空格、数字及其他字符的个数。
可以使用C语言的字符数组和循环语句来实现该功能,具体实现如下:
```c
#include <stdio.h>
int main() {
char str[100];
int i, upper=0, lower=0, space=0, digit=0, other=0;
printf("请输入一串字符,以#结束:\n");
for (i = 0; i < 100; i++) {
scanf("%c", &str[i]);
if (str[i] == '#') {
break;
}
if (str[i] >= 'A' && str[i] <= 'Z') {
upper++;
} else if (str[i] >= 'a' && str[i] <= 'z') {
lower++;
} else if (str[i] == ' ') {
space++;
} else if (str[i] >= '0' && str[i] <= '9') {
digit++;
} else {
other++;
}
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("空格:%d\n", space);
printf("数字:%d\n", digit);
printf("其他字符:%d\n", other);
return 0;
}
```
运行程序后,输入一串字符,以#结束,程序将会输出大写字母、小写字母、空格、数字及其他字符的个数。
C语言输入一字符串,分别统计字符串中大写字母和小写字母的个数
以下是使用 C 语言编写的程序,统计字符串中大写字母和小写字母的个数:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
int length;
int uppercase = 0;
int lowercase = 0;
printf("请输入字符串:");
scanf("%s", str);
length = strlen(str);
// 统计大写字母和小写字母的个数
for (int i = 0; i < length; i++) {
if (isupper(str[i])) {
uppercase++;
} else if (islower(str[i])) {
lowercase++;
}
}
printf("大写字母的个数为:%d\n", uppercase);
printf("小写字母的个数为:%d\n", lowercase);
return 0;
}
```
程序首先要求用户输入字符串,然后使用 `strlen` 函数获取字符串的长度。接下来,使用 `for` 循环遍历字符串的每个字符,如果字符为大写字母,则将 `uppercase` 的值加 1;如果字符为小写字母,则将 `lowercase` 的值加 1。最后,输出大写字母和小写字母的个数。在这个程序中,我们使用了 `ctype.h` 中的函数 `isupper` 和 `islower` 来判断字符是否为大写字母或小写字母。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)