Home ยป Python code to the words of a sentence in reverse order

Python code to the words of a sentence in reverse order

Below is the python program to print the given sentence in reverse order

wd=''
rwd=''
str=input("Enter a sentence:")
str+=' '
p=len(str)
for i in range(0,p):
    if str[i]==' ':
        rwd=wd+' '+rwd
        wd=''
    else:
        wd=wd+str[i]
print("Words in reversed order:",rwd)

Contents

Output

Enter a sentence: computer is fun

Words in reversed order: fun is computer