Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

arrays

char

How to find char in a char array by using find function? If I just for loop the vowel then I could have gotten the answer but I'm asked to use std::find.. Thanks.

bool IsVowel (char c) { 

    char vowel[] = {'a', 'e', 'i', 'o', 'u'};            
    bool rtn = std::find(vowel, vowel + 5, c);

    std::cout << " Trace : " << c  << " " << rtn << endl;

    return rtn; 
 }
like image 417
Michael Sync Avatar asked Oct 30 '10 19:10

Michael Sync


1 Answers

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); 
 }
like image 169
Maciej Hehl Avatar answered Oct 23 '22 19:10

Maciej Hehl