Answer to Question #195324 in Python for hemanth

Question #195324

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.

Input

The input will be a single line containing a sentence.

Output

The output should be a single line containing the sentence with the alternate words reversed.

Explanation

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




1
Expert's answer
2021-05-19T15:51:48-0400
def reverseWord(Sentence):
    words = Sentence.split(" ")

    newWords = [word[::-1] for word in words]
    newSentence = " ".join(newWords)
    return newSentence

sentence = input("Please enter the input sentence:")
print(reverseWord(sentence))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS