Question
USING PYTHON Write a function called reverseString( .. ) that takes a string as input argument, and returns a string such that every other word
USING PYTHON Write a function called reverseString( .. ) that takes a string as input argument, and returns a string such that every other word of the input string is reversed while the order of the words remains the same.
I.e.) If the input to the function is "hello there, how are you", then the output should be " olleh there, woh are uoy"
The total number of characters of the input string should match the number of characters of the output string.
Ask the user to :
'Enter a sentence: '
Your program shall use the following function format and special construct to support unit testing and allow you to enter a sentence when running the code.
def reverseString(a_str):
# this is where your function goes
if __name__ == '__main__':
print(reverse_string(input("Enter a sentence: ")))
------------------------------------------------------------------------------------------------------------------------------------------------
This is what I have. How would I change it so instead of reversing all words in the sentence, it reverses every other word in the sentence.
def reverseString(a_str): words = a_str.split(" ") newWords = [word[::-1] for word in words] newSentence = " ".join(newWords) return newSentence
if __name__ == '__main__':
print(reverse_string(input("Enter a sentence: ")))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started