C++ How to find char in a char array by using find function?

bool IsVowel (char c) { 

    char vowel[] = {'a', 'e', 'i', 'o', 'u'};
    char* end = vowel + sizeof(vowel) / sizeof(vowel[0]);            
    char* position = std::find(vowel, end, c);

    return (position != end); 
 }

std::find(first, last, value) returns an iterator to the first element which matches value in range [first, last). If there's no match, it returns last.

In particular, std::find does not return a boolean. To get the boolean you're looking for, you need to compare the return value (without converting it to a boolean first!) of std::find to last (i.e. if they are equal, no match was found).


Simplifying and correcting

inline bool IsVowel(char c) {
    return std::string("aeiou").find(c) != std::string::npos;
}

See a demo http://ideone.com/NnimDH.

Tags:

C++

Arrays

Char