maxphat
maxphat

Reputation: 37

Devowelling an input using getchar() in C

Having a bit of trouble, and am confused as to why this wont work. The code will take the input and print the input without any lower case vowels.

#include <stdio.h>
int main (void) {
    int i=getchar();
        while (i != EOF) {
            if(i!='a' && i!='e' && i!='i'&&i!='o' && i!='u') {
                printf("%c", i);
                i=getchar();    
            }
        }

}   

Upvotes: 0

Views: 577

Answers (1)

eatcrayons
eatcrayons

Reputation: 1582

Check your if statement.

If the input character is not in {a,e,i,o,u} then you print the character and get a new character. But you really want to get a new character every time through the loop. Since i isn't changing when it's a vowel, the code runs forever -- never reaching EOF.

The fix is to move the call to getchar() to just after the if statement.

Upvotes: 1

Related Questions