Reverse Alternate Words in Sentence
Given a sentence, write a program to reverse the alternate words in the sentence and print it.
Reverse the first word and respective alternative words.
The input will be a single line containing a sentence.
The output should be a single line containing the sentence with the alternate words reversed.
For example, if the given sentence is
Sun rises in the east and sets in the west The alternate words which need to be reversed are (Sun, in, east, sets, the), by reversing these words the output should benuS rises ni the tsae and stes in eht west
Sample Input 1
Sun rises in the east and sets in the west
Sample Output 1
nuS rises ni the tsae and stes in eht west
Sample Input 2
The car turned the corner
Sample Output 2
ehT car denrut the renroc
user_str = input('Enter your string: ')
words = user_str.split()
for i in range(0,len(words),2):
words[i] = words[i][::-1]
output_str = ' '.join(words)
print(output_str)
Comments
Leave a comment