A line of text is read from the input terminal into a stack. Write a python program to output the string in reverse order, each character appearing twice. (e.g., the string a  b c d e should be changed to ee dd cc bb aa)

In this post, we are going to read text from input terminal into a stack and write python program to output the string in reverse order,each character appearing twice. For this, we will use two stack operations push () and display() operations. We will use append() method to add character one by one into stack and reverse() method to reverse the characters in the stack. We will also use isEmpty() function to check whether stack is empty or not.

Lets implement the python program using all above function

Python program to output string in reverse order using stack

def isEmpty(stk):
    if stk == []:
        return True
    else :
        return False

def push(stk,item):
    for i in item :        
         stk.append(i+i)

def display(stk):
    if isEmpty(stk):
        return "underflow"
    else:        
        stack.reverse()
        for j in stack :
              print( j , end=" ")
              
stack = []
txt = input("Enter a line of text :")
push(stack,txt)
display(stack)





Output:

Enter a line of text :This is technocrash.online website
ee tt ii ss bb ee ww    ee nn ii ll nn oo .. hh ss aa rr cc oo nn hh cc ee tt    ss ii    ss ii hh TT 

Python program to output string in reverse order without using reverse function

Output


Enter a line of text : this is technocrash.online
ee nn ii ll nn oo .. hh ss aa rr cc oo nn hh cc ee tt ss ii ss ii hh tt

<

Leave a Reply

Your email address will not be published.