1

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]<<" ";
    }
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
XYZ
  • 21
  • 2
  • *But the program compiles fine.* -- That doesn't mean the program has no bugs. A program compiling fine only means there are no syntax errors. The second thing is that you don't need to write code to check for spaces if you had used `std::istringstream` and `operator >>`. – PaulMcKenzie Oct 02 '21 at 12:36
  • I think `cin` is taking only `I` as input. Try using `getline()` After doing that, make sure you iterate vector `s` from the last index to 1st index to print them in reverse order. You might want to make first letter of last element from vector `s` capital. – virteanchi Oct 02 '21 at 12:41

3 Answers3

2

You want to read an entire line into a string. cin >> input will read only the first word of the line. You can use cin.getline() method to read an entire line.

0

first we read a string using cin, we will exit from the stream using the "exit" keyword.

 #include<iostream>
 #include<string> 
 #include<vector>

 using namespace std;


 int main()
  {
     vector<string> s;
     string input;

     for (int i = 0; i < 10; i++) {
         cin >> input;
    
         if (input == "exit") // breaking from the loop
             break;
        
         s.push_back(input); //adding word at the end of vector s
     }


     for (int i = s.size()-1; i >= 0; --i) // starting loop from the last index(s.size()-1)
          cout << s[i] << ' ';
  }

Input :

I like to get answer from StackOverflow

exit

Output :

stackoverflow from answer get to like I
Jitu DeRaps
  • 144
  • 1
  • 9
0
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<string> sentence;
  string input;

  getline(cin, input);

  stringstream s(input);

  while(s >> input)
  {
      sentence.push_back(input);
  }

  std::reverse(sentence.begin(), sentence.end());

  for(const auto& word : sentence)
      std::cout << word << " ";

  return 0;
}
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Oct 02 '21 at 15:13