You are given an input sentence and you have to reverse the sentence not by reversing all the letters of the string but by reversing the words in the sentence only.
For example:
Consider the input string:
"words are reversed but not letters in words"
The output should be:
"words in letters not but reversed are words" (do not mind the quotes)
Python Implementation:
1. Get the input string in a variable
s='words are reversed but not letters in words'
2. split the string and put it in a list
s=s.split(' ')
3. Reverse the list
s.reverse()
or
s=s[::-1]
4. Join the items of the list using space seperator to get the reversed sentence or desired output
print ' '.join(s)
or
s=' '.join(s)
print s
Pythonic Way:
print ' '.join("words are reversed but not letters in words".split(' ')[::-1])
or
print ' '.join(raw_input().split(' ')[::-1])
For example:
Consider the input string:
"words are reversed but not letters in words"
The output should be:
"words in letters not but reversed are words" (do not mind the quotes)
Python Implementation:
1. Get the input string in a variable
s='words are reversed but not letters in words'
2. split the string and put it in a list
s=s.split(' ')
3. Reverse the list
s.reverse()
or
s=s[::-1]
4. Join the items of the list using space seperator to get the reversed sentence or desired output
print ' '.join(s)
or
s=' '.join(s)
print s
Pythonic Way:
print ' '.join("words are reversed but not letters in words".split(' ')[::-1])
or
print ' '.join(raw_input().split(' ')[::-1])