main.cpp 1.5 KB

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