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.
#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;
}
@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?