#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, const char * argv[]) {
ifstream file("file.txt");
if (!file) {
std::cout << "Unable to open file" << std::endl;
}
string separators = " \t\r\n,.!?;:";
int i = 0;
char c;
bool lastWasSep = true;
std::string tokens[4];
std::string curToken;
while (file.get(c)) {
bool isSep = separators.find(c) != string::npos;
if (lastWasSep == isSep) {
curToken += c;
} else {
tokens[i++] = curToken;
curToken = c;
}
lastWasSep = isSep;
if (i == 4) {
cout << tokens[0] << tokens[3] << tokens[2] << tokens[1];
i = 0;
}
}
tokens[i++] = curToken;
if (i == 4) {
cout << tokens[0] << tokens[3] << tokens[2] << tokens[1];
} else {
for (int j = 0; j < i; j++) {
cout << tokens[j];
}
}
return 0;
}