I have to write a function that prints out all vowels in a given string line including Y, unless Y is followed by another vowel.
For example:
"test phrase" returns 3
"yes no why not?" returns 4
I've tried using getline(), but I'm not sure if there's a way to use it without requiring some type of input from the user. I need it to use an already declared string.
Here's my code that somewhat works, but takes an input.
#include <iostream>
#include <string>
using namespace std;
int numVowelsIncludingY(string line){
int v = 0;
getline(cin, line);
for(int i = 0; i < line.length(); ++i){
if(line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U' || line[i] == 'Y' ||
line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'y'){
++v;
}
}
return v;
}
UPDATE
I've removed getline and reading the string works fine, but I'm still confused on how to count Y as a vowel only if it's not followed by another vowel.
#include <iostream>
#include <string>
using namespace std;
int numVowelsIncludingY(string line){
int v = 0;
for(int i = 0; i < line.length(); ++i){
if(line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U' || line[i] == 'Y' ||
line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'y'){
++v;
}
}
return v;
}