统计元音字母次数c语言以#结束
时间: 2023-12-20 18:06:31 浏览: 29
以下是统计元音字母次数的C语言代码,以#结束:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[1000];
int vowels = 0, consonants = 0;
printf("请输入一个字符串,以#结束:\n");
scanf("%[^#]s", str);
for (int i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else if (ch >= 'a' && ch <= 'z') {
consonants++;
}
}
printf("元音字母的个数为:%d\n", vowels);
printf("辅音字母的个数为:%d\n", consonants);
return 0;
}
```
相关问题
统计元音字母次数c语言
以下是统计元音字母次数的C语言代码:
```c
#include <stdio.h>
#include <string.h>
int count(char str[]) {
int len = strlen(str);
int count = 0;
for (int i = 0; i < len; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
count++;
}
}
return count;
}
int main() {
char str[100];
int cnt;
fgets(str, 100, stdin);
cnt = count(str);
printf("%s,%d", str, cnt);
return 0;
}
```
该程序首先定义了一个count函数,用于统计字符串中元音字母的个数。然后在主函数中,使用fgets函数获取用户输入的字符串,并调用count函数统计元音字母的个数,最后输出原字符串和元音字母的个数。
统计元音字母个数c语言函数
要统计一个字符串中元音字母的个数,可以使用C语言编写一个函数来实现。首先,需要定义一个函数,该函数接受一个字符串作为参数,并返回元音字母的个数。在函数内部,可以使用循环遍历字符串中的每个字符,并使用条件语句判断每个字符是否是元音字母。
在循环内部,可以使用switch语句来检查每个字符是否是元音字母,如果是元音字母,则将计数器加1。最后,函数返回计数器的值,即为字符串中元音字母的个数。
具体实现过程如下:
```c
#include <stdio.h>
int countVowels(char *str) {
int count = 0;
for(int i = 0; str[i] != '\0'; i++) {
switch(str[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
count++;
break;
}
}
return count;
}
int main() {
char str[] = "Hello World";
int result = countVowels(str);
printf("元音字母的个数为: %d\n", result);
return 0;
}
```
以上为一个简单的C语言程序,该程序定义了一个countVowels函数来统计字符串中元音字母的个数,并在main函数中进行了测试。运行程序后,可以得到字符串"Hello World"中元音字母的个数为3。