code.cpp 641 B

1234567891011121314151617181920
  1. /* Write a function that returns the first n vowels of a string.
  2. * Return "invalid" if the n exceeds the number of vowels in
  3. * a string. Vowels are: a, e, i, o, u
  4. */
  5. bool isVowel(char c){
  6. c = tolower(c);
  7. return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
  8. }
  9. std::string firstNVowels(std::string str, int n) {
  10. // all vowels from string are stored here
  11. std::string vowels = "";
  12. // check for vowels in string, if a vowel is found add to vowels
  13. for(int i = 0;i<str.size();i++){
  14. if(isVowel(str[i])){
  15. vowels.push_back(str[i]);
  16. }
  17. }
  18. if(n <= vowels.size()){return vowels.substr(0,n);}
  19. else return "invalid";
  20. }