1234567891011121314151617181920212223242526272829303132 |
- /* A spoonerism is when the first letters / sounds of two words are
- * transposed onto one another. Create a function that takes a two-word
- * string and performs a spoonerism on the phrase.
- * notes for project
- * * Only two words will be parsed into the function. Don't worry about
- * handling more than two.
- * * You won't always just have to swap the first letters,
- * take care to notice which letters have been switched in the examples
- * (notice the difference between vowel-starting and consonant-starting words).
- */
- bool isVowel(char c){
- c = tolower(c);
- return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
- }
- std::string spoonerise(std::string phrase) {
- // split the phrase into two words
- std::size_t p = phrase.find(' ');
- std::string fw = phrase.substr(0,p), sw = phrase.substr(p+1);
- // define two prefixes
- std::string fp = "", sp = "";
- // remove characters off 1st word, put into prefix 1.
- while(!isVowel(fw[0])){
- fp.push_back(fw[0]);
- fw.erase(fw.begin());
- }
- // remove characters off 2nd word, put into prefix 2.
- while(!isVowel(sw[0])){
- sp.push_back(sw[0]);
- sw.erase(sw.begin());
- }
- return sp + fw + ' ' + fp + sw;
- }
|