编写C语言程序,输入一个以回车符结束的字符串(少于80个字符),统计并输出其中大写辅音字母的个数(大写辅音字母是除‘A’,‘E’,‘I’,‘O’,‘U’以外的大写字母)。
时间: 2023-07-24 18:17:59 浏览: 45
好的,下面是你需要的C语言程序代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[80];
int i, count = 0;
printf("请输入一个少于80个字符的字符串:\n");
fgets(str, 80, stdin);
for (i = 0; str[i] != '\0' && str[i] != '\n'; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') { // 判断是否为大写字母
if (str[i] != 'A' && str[i] != 'E' && str[i] != 'I' && str[i] != 'O' && str[i] != 'U') { // 判断是否为辅音字母
count++;
}
}
}
printf("大写辅音字母的个数为:%d\n", count);
return 0;
}
```
这个程序会要求用户输入一个少于80个字符的字符串,并且统计其中大写辅音字母的个数,最后输出结果。程序会自动忽略输入字符串中的回车符。
相关问题
c语言输入一个以回车结束的字符串(少于80个字符),统计其中大写字母的个数。
可以使用C语言标准库函数中的`fgets()`函数读取以回车结束的字符串,然后使用`strlen()`函数获取字符串长度,遍历字符串中每个字符,判断其是否为大写字母,最终统计大写字母的个数。
下面是示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[80];
int len, count = 0;
printf("请输入一个少于80个字符的字符串:");
fgets(str, 80, stdin); // 从标准输入读取字符串
len = strlen(str); // 获取字符串长度
for (int i = 0; i < len; i++) {
if (isupper(str[i])) { // 判断字符是否为大写字母
count++;
}
}
printf("大写字母个数为:%d\n", count);
return 0;
}
```
注意:此代码中使用了标准库函数`isupper()`判断字符是否为大写字母,需要在程序开头加上`#include <ctype.h>`头文件。
C语言编写程序输入一个以回车符结束的字符串,分别统计其中英文字母、数字、其他字符的个数
好的,这是一个比较基础的 C 语言编程问题。下面是一个简单的程序,可以实现输入一个以回车符结束的字符串,然后统计其中英文字母、数字、其他字符的个数:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int letter_count = 0, digit_count = 0, other_count = 0;
printf("请输入一个字符串:");
fgets(str, 100, stdin); // 读入一个以回车符结束的字符串
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) { // 如果是英文字母
letter_count++;
} else if (isdigit(str[i])) { // 如果是数字
digit_count++;
} else { // 其他字符
other_count++;
}
}
printf("其中英文字母个数为:%d,数字个数为:%d,其他字符个数为:%d\n", letter_count, digit_count, other_count);
return 0;
}
```
在这个程序中,我们首先定义了一个字符数组 `str`,用于存储输入的字符串。然后定义了三个变量 `letter_count`、`digit_count`、`other_count`,分别用于统计英文字母、数字、其他字符的个数,初始值都为 0。
接下来使用 `fgets` 函数读入一个以回车符结束的字符串,最多读入 99 个字符(因为还要留一个位置存储字符串末尾的空字符)。
然后使用一个 for 循环遍历字符串中的每个字符,使用 `isalpha` 和 `isdigit` 函数判断该字符是不是英文字母或数字,从而分别累加统计变量的值。如果既不是英文字母也不是数字,则认为是其他字符,也进行累加。
最后输出三个统计变量的值即可。