code.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. /* A spoonerism is when the first letters / sounds of two words are
  2. * transposed onto one another. Create a function that takes a two-word
  3. * string and performs a spoonerism on the phrase.
  4. * notes for project
  5. * * Only two words will be parsed into the function. Don't worry about
  6. * handling more than two.
  7. * * You won't always just have to swap the first letters,
  8. * take care to notice which letters have been switched in the examples
  9. * (notice the difference between vowel-starting and consonant-starting words).
  10. */
  11. bool isVowel(char c){
  12. c = tolower(c);
  13. return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
  14. }
  15. std::string spoonerise(std::string phrase) {
  16. // split the phrase into two words
  17. std::size_t p = phrase.find(' ');
  18. std::string fw = phrase.substr(0,p), sw = phrase.substr(p+1);
  19. // define two prefixes
  20. std::string fp = "", sp = "";
  21. // remove characters off 1st word, put into prefix 1.
  22. while(!isVowel(fw[0])){
  23. fp.push_back(fw[0]);
  24. fw.erase(fw.begin());
  25. }
  26. // remove characters off 2nd word, put into prefix 2.
  27. while(!isVowel(sw[0])){
  28. sp.push_back(sw[0]);
  29. sw.erase(sw.begin());
  30. }
  31. return sp + fw + ' ' + fp + sw;
  32. }