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

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay