1

I am trying to reverse the words in a string using this code:

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

int main()
{
    //_ _ the sky is blue

    string vec;
    getline(cin, vec);
    stack<string> str;

    string temp = "";
    string ans = "";

    for (int i = 0; i < vec.length(); i++)
    {
        if (vec.at(i) == ' ')
        {
            if (temp.length() > 0)
            {
                str.push(temp);
                temp = "";
            }
            else
            {
                temp = temp + vec.at(i);
            }
        }
    }

    //ans = ans + temp;

    while (!str.empty())
    {
        ans = ans + " " + str.pop();
    }

    if (ans.length() != 0 && ans.at(0) == ' ')
        ans = ans.substr(1);

    cout << ans << endl;
}

I'm receiving this error at line 33 telling "no match for 'operator+'".

I have attached the relevant screenshot:

enter image description here

Please, help.

anastaciu
  • 20,013
  • 7
  • 23
  • 43
RafiaChy
  • 45
  • 7

3 Answers3

2

pop() is a stack member method with void return type, it doesn't return a string, therefore it cannot be printed neither can it be concatenated with other strings.

As the error shows you you can't add void and string using + operator for these 2 different types (unless you made that option available by overloading the + operator), so ans = ans + " " + str.pop(); is wrong.

You could use:

while (!str.empty())
{
    ans = ans + " " + str.top();
    str.pop();
}

As top() does return a string object.


I should point out that using #include <bits/stdc++.h> is bad and using namespace std is also not very good, but bringing them together is a disaster waiting to happen.

anastaciu
  • 20,013
  • 7
  • 23
  • 43
1

The method pop of the container adapter std::stack has the return type void. So this statement

ans= ans+" "+str.pop();

is incorrect and the compiler will issue an error.

You need to write something like

while(!str.empty()){
    ans= ans+" "+ str.top();
    str.pop();
}

Pay attention to that this for loop

 for(int i=0 ; i<vec.length(); i++){
  if(vec.at(i)==' '){
   if(temp.length()>0){
    str.push(temp);
    temp = "";
   }

   else{
    temp = temp + vec.at(i);
   }
  }
}

has a bug. If the string stored in the object vec does not end with a space character then the last word of the string will not be pushed on the stack.

It seems what you are trying to do is the following.

#include <iostream>
#include <string>
#include <stack>
#include <cctype>

int main() 
{
    std::string s( "   Hello World   " );
    std::stack<std::string> st;
    
    std::cout << "\"" << s << "\"\n";
    
    for ( std::string::size_type i = 0; i < s.size(); )
    {
        std::string tmp;
        
        while ( i < s.size() && isspace( ( unsigned char )s[i] ) )
        {
            tmp += s[i++];
        }
        
        if ( !tmp.empty() ) 
        {
            st.push( tmp );
            tmp.clear();
        }
        
        while ( i < s.size() && !isspace( ( unsigned char )s[i] ) )
        {
            tmp += s[i++];
        }
        
        if ( !tmp.empty() )
        {
            st.push( tmp );
        }
    }
    
    std::string result;
    
    while ( !st.empty() )
    {
        result += st.top();
        st.pop();
    }
    
    std::cout << "\"" << result << "\"\n";
    
    return 0;
}

The program output is

"   Hello World   "
"   World Hello   "
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
  • Actually, I agree with replacing str.pop() with str.top(). However, my code is working fine with vec. – RafiaChy Apr 06 '21 at 10:25
  • @RafiaChy You are mistaken If a string like for example "1 2" does not end with a space then the word "2" will not be pushed on the stack. – Vlad from Moscow Apr 06 '21 at 10:27
  • @RafiaChy Another problem is that the user can separate words in a string for example with the tab character '\t'. In this case your program will not work. Also it does not preserve the number of white spaces between words. – Vlad from Moscow Apr 06 '21 at 10:29
-1

Thank you guys for helping me out. Here's code and working perfectly fine:

#include <bits/stdc++.h>

using namespace std;

int main(){

//_ _ the sky is blue

string vec;
getline(cin, vec);
stack<string>str;
string rs;

string temp="";
string ans= "";

for(int i=0 ; i<vec.length(); i++){
if(vec.at(i)==' '){
   if(temp.length()>0){
    str.push(temp);
    temp = "";
   }
}
   else{
    temp = temp + vec.at(i);
   }
}


     ans = ans + temp;

    while(!str.empty()){
        ans= ans+" "+str.top();
        str.pop();

    }

    if(ans.length() != 0 && ans.at(0) == ' '){
      ans = ans.substr(1);
   }

cout<<ans<<endl;
reverse( ans.begin(), ans.end());
cout<<ans<<endl;
}

Here is the output which only allows single space between each words and eliminates both leading and trailing space: enter image description here

RafiaChy
  • 45
  • 7