String from array

closed account (G60iz8AR)
How do you get a string from array that has 2 or more vowels from a e i o u in it ?

// YOU FILL IN THIS METHOD TO RETURN TRUE IF AND ONLY IF
// THE WORD CONTAINS 2 OR MORE VOWELS FROM THESE 'a', 'e', 'i, 'o' ,'u'

bool containsVowels( string word )
{
return false; // just to compile - change as needed
}
Are we just doing your homework here?

This isn't difficult; make a for loop to loop through the characters of 'word' and use if statements to check for vowels. Increment a local int variable each time a vowel is encountered. At the end of the function return true if the int variable is >= 2.
I don't find your question to be very clear... Anyway, I will answer what I think you are asking.

You can just loop through the array to find if it has any vowels. Here is an example which finds out if the array has at least 2 of r,s,t present:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>

using std::cout;

int main()
{
    char arr[5] = {'t','r','e','a','t'};//This is the array we are going to search. 
    int x = 0, total = 0;
    bool l[3] = {0}; // l[0] represents if there is an r, l[1] represents if there is an s...
    for(x = 0; x < 5; ++x)
    {
        switch(arr[x]) //Duplicates won't count double, as it will just keep the bool value at 1. 
        {
            case 'r':
            case 'R':
                l[0] = 1; //Yes, there is at least one r. 
                break;
            case 's':
            case 'S':
                l[1] = 1; //Yes there is at least one s. 
                break;
            case 't':
            case 'T':
                l[2] = 1; //Yes there is at least one t. 
                break;
            default:
                break;
        }
    }
    for(x = 0; x < 3; ++x) //Count how many letters we found were present. 
    {
        if(l[x] > 0)
        {
            ++total;
        }
    }
    if(total >= 2)
    {
        cout << "At least two letters from r,s & t appear.\n";
    }
    else
    {
        cout << "Less than two letters from r,s & t appear.\n";
    }
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool doesContainVowel( string word )
{
    int noOfVowels = 0;
    
    for ( int i = 0; i < word.size(); i++ ) {
        switch ( word[i] ) {
            case 'a' :
            case 'e' :
            case 'i' :
            case 'o' :
            case 'u' :
                noOfVowels++;
                if ( noOfVowels >= 2 ) return true;
                //break;
        }
    }
    return false;
}
Last edited on
@shadow fiend - Your method would not work. Say for example they array was Mississippi, your function would return true, when the correct answer is false (if I am understanding the OP's question correctly).
why would the correct return value be false, if i'm not mistaken he wants to return true if the number of vowel become greater than or equal to 2 ? and my function would return true on the 5th run of the loop if the word was mississippi
Mats I believe is trying to return how many *unique* letters from a set are present in a string, whereas shadow fiend interprets the problem as "count how many of a string's letters exist within a set." I'm inclined to agree with shadow fiend's interpretation. OP, can you provide clarification?
Last edited on
Topic archived. No new replies allowed.