用c语言统计字符串中元音字母的个数
时间: 2024-12-04 14:24:22 浏览: 89
可以通过循环遍历字符串,判断每个字符是否为元音字母来统计元音字母的个数。C语言中,元音字母包括'A'、'E'、'I'、'O'和'U',可以使用switch语句来实现。以下是示例代码:
```
#include <stdio.h>
#include <string.h>
int count_vowels(char *str) {
int count = 0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
switch (str[i]) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
count++;
break;
default:
break;
}
}
return count;
}
int main() {
char str[100];
printf("请输入字符串:");
scanf("%s", str);
int count = count_vowels(str);
printf("元音字母个数为:%d\n", count);
// 相关问题
printf("
阅读全文
相关推荐

















