Stacks In Python 9

Example:1
Write a python program to read a line of text and Print it in reverse (Reverse word by word).
Sol:
example:
Input line:
hello how are you

output:
you are how hello

line=input("Enter any line of text ")
stack=[]
for word in line.split():
    print(word)
    stack.append(word)
print("stack ",stack)
print("total words are ",len(stack))
n=len(stack)
for i in range(n-1,-1,-1):
    print(stack[i],end=' ')

Output:

Enter any line of text hello how are you
hello
how
are
you
stack  ['hello', 'how', 'are', 'you']
total words are  4
you are how hello 

Example:2
Write a python program to read a line of text and Print it in reverse.
Sol:

example:
Input line:
hello how are you

output:
uoy era woh olleh

line=input("Enter any line of text ")
stack=[]
for word in line:
    print(word)
    for ch in word:
        stack.append(ch)
print("stack ",stack)
print("total words are ",len(stack))
n=len(stack)
for i in range(n-1,-1,-1):
    print(stack[i],end='')

Output:

Enter any line of text hello how are you
h
e
l
l
o
 
h
o
w
 
a
r
e
 
y
o
u
stack  ['h', 'e', 'l', 'l', 'o', ' ', 'h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u']
total words are  17
uoy era woh olleh

Visitor Counter

0183269