12345678910111213141516171819 |
- /* Create a function that takes a string and returns a new string with
- * its first and last characters swapped, except under two conditions:
- * If the length of the string is less than two, return "Incompatible.".
- * If the first and last characters are the same, return "Two's a pair.".
- */
- std::string flipEndChars(std::string str) {
- // first check string size, if less than 2 return "Incompatible.".
- if(str.size()<2){
- return "Incompatible.";
- }
- // check to see if first character = last character if true return "Two's a pair."
- if(str[0]==str[str.size()-1]){
- return "Two's a pair.";
- }
- // swap first and last characters
- std::swap(str[0],str[str.size()-1]);
- return str;
- }
|