8

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; 
 }

5 Answers 5

7
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); 
 }
Sign up to request clarification or add additional context in comments.

Comments

4

Simplifying and correcting

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

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

1 Comment

This is very late, but std::string allocates data on the heap, which is slower than allocating on the stack.
3

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).

2 Comments

Do I have to use char::iterator? I think there is no such things as char:iterator, right? What kind of iterator should I use? And how to check whether it's end or not? Normally, if it's a string, we can do like s.end() == it but array doens't have .end().
char* (pointer to char) is an iterator in this context. Your end in your example is vowel+5. It would, of course, be somewhat easier (or less complex) to use std::vector or std::string.
0

Use:

size_t find_first_of ( char c, size_t pos = 0 ) const;

Reference: http://www.cplusplus.com/reference/string/string/find_first_of/

1 Comment

The question was about an an array of characters not an std::string
-2

Looking for the sequence of a character in an array - why drag bool into it. Give a simple straightforward answer if you can.

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    //Position
    int posc(char suit[], char fc, int sz)
        {
            int i = 0;
            do
            {
                if (toupper(fc) == suit[i]) return i;
                else
                i = i + 1;
            } 
            while (i < sz);
            cout << "\n Character " << fc << " not available in list";
            return 99;
        }
    int main()
    {
        char suit[] = { 'S', 'D', 'C', 'H' };
        char fc;
        int res;
        int sz = size(suit);
        cout << "\n Enter a character: ";
        cin >> fc;
        res = posc(suit, fc, sz);
        cout << "\n Sequence no: " << res;
        return 0;
    }

The main & cout frills are added just to show a working solution

3 Comments

Could you please provide more details about your solution?
See above - all that it spits out is a sequence number.
The original question asked about how to use std::find for this. I don't see you using this function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.