I'm trying to write a code to reverse the a sentence. For example,
Input - "I like to get answer from stackoverflow"
Output - "Stackoverflow from answer to get like I"
I wrote the following code but somehow when I add a string to the vector it is not working. But the program compiles fine. Can someone help me out. Below is the code
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> s;
string input;
cin>>input;
string temp="";
for(int i=0;input[i]!='\0';i++)
{
if(input[i]==' ')
{
s.push_back(temp);
temp="";
}
else
{
temp=temp+input[i];
cout<<temp<<endl;
}
}
for(int i=0;i<s.size();i++)
{
cout<<s[i]<<" ";
}
}
std::istringstreamandoperator >>.cinis taking onlyIas input. Try usinggetline()After doing that, make sure you iterate vectorsfrom the last index to 1st index to print them in reverse order. You might want to make first letter of last element from vectorscapital.