-2

Input

"the sky is blue"

Expected Output

"blue is sky the"

My Output

"blue is sky "

I am unable to point out the error in the code.

Here is the code :

#include <bits/stdc++.h>
using namespace std;

int main() {
    string s = "the sky is blue";
    reverse(s.begin(),s.end());
    stack<char> t;
    for(int i = 0;i < s.length();i++){
        if(s[i] != ' '){
            t.push(s[i]);
        }
        else{
            while(!t.empty()){
                cout << t.top();
                t.pop();
            }
            cout << " ";
        }
    }
    return 0;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
A.Vik
  • 93
  • 7
  • `reverse` does not reverse each word. You should have seen that if you debugged your code. – PaulMcKenzie Sep 28 '17 at 05:32
  • I know that.After reversing I am printing out the elements using a stack. – A.Vik Sep 28 '17 at 05:33
  • Which will reverse each word... – A.Vik Sep 28 '17 at 05:33
  • eulb si yks eht – A.Vik Sep 28 '17 at 05:34
  • Why not make this simple and use `std::istringstream` instead of writing a loop checking for a space? This is really no more than a 5 line program, without all of the checking required for a space character. – PaulMcKenzie Sep 28 '17 at 05:35
  • This is the output after using reverse.After pushing them into a stack until I encounter a space,I print them while popping them out which gives: blue is sky – A.Vik Sep 28 '17 at 05:35
  • 1
    [Example of std::istringstream](http://coliru.stacked-crooked.com/a/07e74bb8b65a975d). No need to check for spaces. So before posting any answers and getting the "my teacher or book hasn't taught me this, so I can't use it", is this acceptable? – PaulMcKenzie Sep 28 '17 at 05:43
  • 1
    Unrelated: Watch out for the `#include using namespace std;` combination. It can lead to some nearly incomprehensible errors. More on why here https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h and here https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice . Together you have the entire standard library in your global namespace. That's a lot of potential landmines to step on. – user4581301 Sep 28 '17 at 05:44
  • Thanks,@PaulMcKenzie ,Now implementing using istringstream – A.Vik Sep 28 '17 at 06:10
  • You don know there's a far more efficient in-place method? – Deduplicator Jul 28 '18 at 10:25

2 Answers2

1

You push "eht" into the stack, but you don't start poping it, since the length of the string doesn't allow you to, since the for loop stops executing.

Pop it after the for loop, like this:

#include <string>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
  string s = "the sky is blue";
  reverse(s.begin(),s.end());
  stack<char> t;
  for(unsigned int i = 0;i < s.length();i++){
    if(s[i] != ' '){
      t.push(s[i]);
    }
    else {
      while(!t.empty()){
        cout << t.top();
        t.pop();
      }
      cout << " ";
   }
  }
  while(!t.empty()){
    cout << t.top();
    t.pop();
  }
}

Output:

blue is sky the

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

C++ program to reverse words in string using Stack data structure.

E.g. "my.name.is.vivek.mutha" gives the output as "mutha.vivek.is.name.my".

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s,s1;
    std::stack<std::string> st;
    int k=0;
    cin>>s;
    int l = s.length();
    vector<string> str;
    for(int i=0;i<l;i++)
    {
        if(s[i]=='.'){
            str.push_back(s1);
            str.push_back(".");
            s1="";
        }
        else
            s1.append(s,i,1);
    }
    str.push_back(s1);
    for(int i=0;i<str.size();i++)
        st.push(str[i]);
    while(!st.empty()){
        cout<<st.top();
        st.pop();
    }
}
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40