Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : reverse-cpp.cpp
- // Author : Francesco Laurita
- // Version :
- // Copyright : Francesco Laurita
- // Description :
- //============================================================================
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <math.h>
- #include <vector>
- using namespace std;
- string apply(string in, int pos){
- int slen = in.length();
- if (slen <= pos){
- return in;
- }else{
- int mid = ceil(slen / 2);
- string right = in.substr(mid,slen);
- string left = in.substr(0,mid);
- return apply(right,pos) + apply(left,pos);
- }
- }
- int main(int argc, char **argv) {
- ifstream fp(argv[1]);
- if (!fp){
- cerr << "Unble to open file!" << endl;
- return 1;
- }
- int nlines;
- fp >> nlines;
- fp.ignore();
- for (int i = 0; i < nlines; i++){
- string line,tok;
- getline(fp,line);
- stringstream ss(line);
- vector<string> tokens;
- int j = 1;
- while (ss >> tok){
- tokens.push_back(apply(tok,j));
- j++;
- }
- for (vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it){
- cout << *it;
- if (it != tokens.end()-1)
- cout << " ";
- }
- tokens.empty();
- cout << endl;
- }
- fp.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement