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]<<" ";
    }
}
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 >>. Commented Oct 2, 2021 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. Commented Oct 2, 2021 at 12:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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;
}

1 Comment

While this code may solve the question, including an explanation 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.