code.cpp 691 B

12345678910111213141516171819
  1. /* Create a function that takes a string and returns a new string with
  2. * its first and last characters swapped, except under two conditions:
  3. * If the length of the string is less than two, return "Incompatible.".
  4. * If the first and last characters are the same, return "Two's a pair.".
  5. */
  6. std::string flipEndChars(std::string str) {
  7. // first check string size, if less than 2 return "Incompatible.".
  8. if(str.size()<2){
  9. return "Incompatible.";
  10. }
  11. // check to see if first character = last character if true return "Two's a pair."
  12. if(str[0]==str[str.size()-1]){
  13. return "Two's a pair.";
  14. }
  15. // swap first and last characters
  16. std::swap(str[0],str[str.size()-1]);
  17. return str;
  18. }