DEV Community

JAIKUMAR DEWANGAN
JAIKUMAR DEWANGAN

Posted on

Stack

#include<bits/stdc++.h>

using namespace std;

void addStrIntoStack( string str)
{ 
    int n = str.length(); 
    string rev = "";
    stack <char>st ; 

    for(int i=0; i<n; i++)
    {
       st.push(str[i]) ;
    }

  // reverse 
  while(!st.empty())
  {
     if(st.top() == 'a' ||st.top() == 'e' || st.top() == 'i'||st.top() == 'o' ||st.top() == 'u' )
      {
           char ch = st.top() - 32;
             rev = rev + ch;
      } 
  else{ rev = rev + " " + st.top();}

    st.pop(); 
  }

  cout<<"string hai "<<rev; 

}

int main(){

  string s = "abcde" ; 

   addStrIntoStack( s);






    return 0; 
}
Enter fullscreen mode Exit fullscreen mode
#include <bits/stdc++.h>

using namespace std;

stack<char> addStrIntoStack(string str)
{
    int n = str.length();
    string rev = "";
    stack<char> st;

    for (int i = 0; i < n; i++)
    {
        st.push(str[i]);
    }

    // reverse
    return st;
}

string reverseStack(stack<char> &st, string rev)
{
    if (st.empty())
    {
        return rev;
    }

 rev = rev + st.top();
    st.pop();

    return reverseStack(st, rev);
}

int main()
{

    string s = "abcde";

    stack<char> st = addStrIntoStack(s);

    string rev = "";

    rev = reverseStack(st, rev);

    cout <<"ye hai :"<< rev;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode
#include <bits/stdc++.h>
using namespace std;

string prefix_to_infix(string str){
    stack<string> st;

    for(int i=str.length()-1;i>=0;i--) {
        if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
            string s ="";
            s+=(str[i]); // string me nhi aa rha tha 
            st.push(s);
        }
        else{
            string a = st.top();
            st.pop();
            string b = st.top();
            st.pop();
            string c = '('+a+str[i]+b+')';
            st.push(c);
        }
    }
    return st.top();
}
int main(){

    string s ;
    cin>>s;
    string str = prefix_to_infix(s);
    cout<<str;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)