#include <iostream>
#include <string>
int main() {
std::string str{"first word and last"};
int ind = str.find(' ');
std::string first_word = str.substr(0, ind);
int ind2 = str.rfind(' ');
std::string last_word = str.substr(ind2+1);
str.replace(0, ind, last_word);
str.replace(ind2, last_word.length(), first_word);
std::cout << str;
return 0;
}