c语言 输入一个字符串,统计这个字符串的元音字母的个数,并输出。求源代码

兄弟们帮我讲解下,c语言 输入一个字符串,统计这个字符串的元音字母的个数,并输出。求源代码
最新回答
↗雾里↖看花☆

2024-10-14 07:47:34

英文中的元音字母只有a、e、i、o、u五个,但要考虑大小写问题。代码如下:

#include "stdio.h"
int main(int argc,char *argv[]){
int n,i;
char s[301],t;
printf("Input a string...\n");
scanf("%300s",s);
for(n=i=0;s[i];i++)
        /*以下判断语句完成大小写元音字母都统计功能*/
if((t=s[i]|0x20)=='a' || t=='e' || t=='i' || t=='o' || t=='u')
n++;
printf("\nA total of %d vowel(s) in this string.\n\n",n);
return 0;
}

运行样例如下:

夜莺与鲸

2024-10-14 07:30:11

int    myfun()
{
    int    i,x;
    i=0;
    x=0;
    char   s[100];
    printf("请输入字符串:");
    scanf("%[^\n]s",s);
    while(1)
    {
        if (s[i]==0)
            break;
        switch    s[i]
        case    'a':
        case    'e':
        case    'i':
        case    'o':
        case    'u':
            x=x+1;
        default:
            i=i+1;
    }
    printf("元音字母的个数为:%d\n",x);  
    return    x;
}
若樱落如烟

2024-10-14 07:55:31

#include <stdio.h>
int main()
{
    char c[100];
    int i, n=0;
    scanf("%s",c);
    for(i=0; c[i]!='\0'; i++){
        if(c[i]=='a'||c[i]=='e'||c[i]=='i'||c[i]=='o'||c[i]=='u')
            n++;
    }
    printf("Num=%d",n);
}