C布尔类型函数始终返回FALSE

时间:2018-11-11 09:23:31

标签: c

我的任务是制作一个带有字符串并输出的程序:

the number of characters in the string
the number of vowels in the string
the number of UPPERCASE letters in the string
the number of lowercase letters in the string
the number of other characters in the string

我们不允许使用ctype.h库。 现在,我只是想输出元音的数量。

#include<stdio.h>
#include<string.h>
#include<stdbool.h>

bool isVowel(char *c);

int main(){
    char userString[5];
    int i;
    int vowelCount;
    char *c;
        printf("enter string:");
        scanf("%c", userString);
            for(i=0; i<= 4; ++i){
                userString[i] = *c;
                isVowel(c);
                if(isVowel(c)){
                    vowelCount = vowelCount + 1;
                }
            }
    printf("%d\n", vowelCount);
return 0;
}

bool isVowel(char *c){
    if(*c == 'a' || *c == 'A' || *c == 'e' || *c == 'E' || *c == 'i' || *c 
== 'I' || *c == 'o' || *c == 'O' || *c == 'u' || *c == 'U' ){
        return true;
    }
    else{
        return false;
    }
}

我相信isVowel总是返回false,因为当我使用输入“ test!”运行它时,我得到了:

enter string: test!
0

2 个答案:

答案 0 :(得分:1)

您没有设置c变量。我怀疑这行:

userString[i] = *c;

应该是这样的:

c = userString + i;

答案 1 :(得分:0)

这是您的代码,并进行了一些更正:

#include<stdio.h>
#include<string.h>
#include<stdbool.h>

bool isVowel(char c);

int main(){
    char userString[5];
    int i;
    int vowelCount=0;
    char c;
        printf("enter string:");
        scanf("%s", userString); // %s
            for(i=0; i<= 4; ++i){
                c=userString[i] ;              
              printf("%c",c);

                if(isVowel(c)){
                    vowelCount = vowelCount + 1;
                } 
            }
    printf("%d\n", vowelCount);
return 0;
}

bool isVowel(char c){
    return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || 
    c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U' ) ; 
}