1234567891011121314151617181920 |
- /* Write a function that returns the first n vowels of a string.
- * Return "invalid" if the n exceeds the number of vowels in
- * a string. Vowels are: a, e, i, o, u
- */
- bool isVowel(char c){
- c = tolower(c);
- return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
- }
- std::string firstNVowels(std::string str, int n) {
- // all vowels from string are stored here
- std::string vowels = "";
- // check for vowels in string, if a vowel is found add to vowels
- for(int i = 0;i<str.size();i++){
- if(isVowel(str[i])){
- vowels.push_back(str[i]);
- }
- }
- if(n <= vowels.size()){return vowels.substr(0,n);}
- else return "invalid";
- }
|