用c语言写一个程序,从键盘输入一串字符,分别找出其中的大写字母,小写字母,数字和其他字符的个数
时间: 2024-05-14 10:13:22 浏览: 95
好的,这是一个关于 C 语言的编程问题。可以使用 getchar() 函数逐个读取输入的字符,并使用 isalpha() 和 isdigit() 函数判断字符是否为字母或数字,然后统计各类字符的个数即可。以下是一个简单的代码示例:
```
#include <stdio.h>
#include <ctype.h>
int main() {
int uppercase = 0, lowercase = 0, digit = 0, other = 0;
char c;
printf("请输入一串字符:\n");
while ((c = getchar()) != '\n') {
if (isupper(c)) {
uppercase++;
} else if (islower(c)) {
lowercase++;
} else if (isdigit(c)) {
digit++;
} else {
other++;
}
}
printf("大写字母:%d\n", uppercase);
printf("小写字母:%d\n", lowercase);
printf("数字:%d\n", digit);
printf("其他字符:%d\n", other);
return 0;
}
```
希望这个代码对您有所帮助!
相关问题
用C语言写出以下程序:输入一行字符串,统计其中大写字母、小写字母、数字和其它字符的个数。
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, upper=0, lower=0, digit=0, other=0;
printf("Enter a string: ");
fgets(str, 100, stdin);
for(i=0; str[i]!='\0'; i++) {
if(isupper(str[i]))
upper++;
else if(islower(str[i]))
lower++;
else if(isdigit(str[i]))
digit++;
else
other++;
}
printf("Uppercase letters: %d\n", upper);
printf("Lowercase letters: %d\n", lower);
printf("Digits: %d\n", digit);
printf("Other characters: %d\n", other);
return 0;
}
```
代码解释:
- `fgets()` 函数用于从标准输入(键盘)读取字符串。
- `isupper()`、`islower()` 和 `isdigit()` 函数用于判断字符是否为大写字母、小写字母或数字。
- `other` 变量用于统计其他字符的个数。它的初值为 0,每次遇到不是大写字母、小写字母和数字的字符时,就将它加 1。
输入一串字符 找出其中的大写字母 小写字母 空格 数字 其他字符各有多少并将一个字符串逆序储存C语言
好的,针对您的问题,我可以给出以下代码示例:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int upper = 0, lower = 0, space = 0, digit = 0, other = 0;
printf("请输入一串字符:");
fgets(str, 100, stdin);
int len = strlen(str);
for (int i = 0; i < len; i++) {
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小写字母:%d\n空格:%d\n数字:%d\n其他字符:%d\n", upper, lower, space, digit, other);
strrev(str);
printf("逆序字符串为:%s\n", str);
return 0;
}
```
这段代码可以接收用户输入的一串字符,然后统计其中大写字母、小写字母、空格、数字和其他字符的个数,并将字符串逆序储存。其中,`fgets()` 函数可以读取用户输入的字符串,`strlen()` 函数可以获取字符串长度,`strrev()` 函数可以将字符串逆序,具体的实现细节可以参考代码注释。
阅读全文
相关推荐













