Finding Vowels in a String Array

I have to write a function that will take in a string array and will then return the first vowel found, if none return -1.
I wanted to make sure the actual code itself worked that it was finding a vowel.
So my question is how do I output the character that the first vowel shows up on?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstring>

using namespace std;

int main()
{

   char vowels[] = {'a', 'e', 'i', 'o', 'u',
                    'A', 'E', 'I', 'O', 'U'};

    string text;
    cout << "Enter a word: ";
    getline (cin, text);


    if (text.find_first_of (vowels) != string::npos)
    {

        cout << "Word has vowels";
    }
    else{
        cout << "Word has no vowels";
    }

    return 0;
}
Last edited on
Perhaps you could use the value returned by find_first_of()?

If you could, can you show me an example of how I would do that?
I was just doing something like this a few days ago, I created a function to identify a letter, and then called it with just vowels

int NumCharFound(string stringToSearch, char letterToFind)
{
int numCharFound = 0;
size_t charOffset = stringToSearch.find(letterToFind, 0);
while (charOffset != string::npos)
{
++numCharFound;
charOffset = stringToSearch.find(letterToFind, charOffset + 1);
}
return numCharFound;
}

then in main call:

int numVowels = NumCharFound(testCompare, 'a');
numVowels += NumCharFound(testCompare, 'e');
numVowels += NumCharFound(testCompare, 'i');
numVowels += NumCharFound(testCompare, 'o');
numVowels += NumCharFound(testCompare, 'u');
numVowels += NumCharFound(testCompare, 'y');

cout << "There are " << numVowels << " vowels found in " << sampleTest << endl;
Registered users can post here. Sign in or register to post.