I'm trying to write a program that reads a text from a file (only strings) and reverse it. The following code does that, but it doesn't take into account spaces between words:
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main(){
ifstream file("file.txt");
char i;
int x;
vector<char> vec;
if(file.fail()){
cerr<<"error"<<endl;
exit(1);
}
while(file>>i){
vec.push_back(i);
}
x=vec.size();
reverse(vec.begin(), vec.end());
for(int y=0; y<x; y++){
cout<<vec[y];
}
return 0;
}
If the text on the file is "dlroW olleH", the program would print out "HelloWorld". What can I do so that it prints "Hello World" (with the space between both words)?