Q:

Finding the index of a char in C using pointers

I'm having difficulties with a program I've been instructed to write. The program should search a word for the first vowel that appears, it then should print out the index of that vowel. If there are no vowel's, it should return -1.
This is my code so far:
int firstVowel(char* string){
//Variable for case with no vowels
int notInString = -1;
int length = strlen(string);
int i;
for(i = 0; i <= length; i+=1){
    if(*string == 'a' || *string == 'e' || *string == 'i' ||
    *string == 'o' || *string == 'u'){
        return i;
    }
else if(*string != 'a' || *string != 'e' || *string != 'i' || *string != 'o' ||
*string != 'u') {
    return notInString;
}
}
}

When I run it with the input "abced" it returns 0 correctly. However, when I run it as fsed it returns -1 incorrectly.

A:

You loop does not increment string and always returns after doing the first comparison, you want
int firstVowel(char* string) {
    //Variable for case with no vowels
    int notInString = -1;
    int length = strlen(string);
    int i;
    for (i = 0; i <= length; i += 1) {
        if (*string == 'a' || *string == 'e' || *string == 'i' ||
            *string == 'o' || *string == 'u') {
            return i;
        } else {
            string++;
        }
    }

    return notInString;
}

This will search the entire array, returning the index of the first vowel it sees.  It cannot return notInString until it has processed the entire string.

