Son Goku
Son Goku

Reputation: 15

(C language) Checking vowels in a String

I am making a vowel checker function. I am somewhat new to the language. I read through that I can not compare a string literal and a character array. Is this true? My program always crashes and I can't see what the problem is. Can anyone explain to me why my code has a problem. Thank you for any kind of input.

void checkVowel(char someString[]){

    int i, len;
    char compare;

    len = strlen(someString);
    printf("%d\n", len);

    printf("you name in all uppercase: ");
    printf(strupr(someString));


    for(i=0; i<len;i++){

        char  compare = someString[i];



        if(compare == "A" || (strcmp(compare,"E"==0))|| compare == "I" || compare == "O" || compare == "U"){
            printf("\n%c", compare);
        }

    }
}

Upvotes: 0

Views: 2528

Answers (2)

ScottK
ScottK

Reputation: 1556

You are using strcmp to compare a character to a C string, that will fail. You should have seen a compiler warning when you tried to do this. Instead of comparing chars and single-character strings, simply compare chars one at a time. No need to use strcmp at all.

Note that using strupr is a non-standard deprecated function provided by Microsoft. Use toupper instead:

#include <ctype.h> // defines toupper and tolower
void checkVowel(char someString[]){
    int i, len;

    len = strlen(someString);
    printf("%d\n", len);

    printf("you name in all uppercase: ");
    printf(strupr(someString));

    for(i=0; i<len;i++) {
       char compare = toupper(someString[i]));
       if ((compare == 'A') || 
           (compare == 'E') || 
           (compare == 'I') || 
           (compare == 'O') ||
           (compare == 'U')) {
            printf("\n%c", compare);
        }
    }
}

Upvotes: 1

pritesh agrawal
pritesh agrawal

Reputation: 1225

You can try with below , I did not run the code , but i hope it would work :

void checkVowel(char someString[]){

int i, len;
char compare;

len = strlen(someString);
printf("%d\n", len);

printf("you name in all uppercase: ");
printf(strupr(someString));


  for(i=0; i<len;i++){

    char vowelArray[5] = [ 'A','E','I','O','U'];
    char  compare = someString[i];
    int j;
    for (j = 0 ; j < 5; j++){

      if(compare == vowelArray[j]){

       printf("\n%c", compare);

      }
    }       

  }
}

Upvotes: 0

Related Questions