Home » All Blogs » Python » Python assignments » M.Sc Computer Application » M.Sc Python Programming Assignment 29
M.Sc Python Programming Assignments
Assignment 29
Question:
Write a Python class to reverse a string word by word. Input string : ‘hello.py’ Expected Output : ‘.py hello’
Code:
class StringReverser:
def reverse_words(self, input_string):
words = input_string.split()
reversed_string = ' '.join(reversed(words))
return reversed_string
# Example usage
input_string = input("Enter a string: ")
reverser = StringReverser()
reversed_string = reverser.reverse_words(input_string)
print("Reversed string word by word:")
print(reversed_string)
Output:
Enter a string: goku is running
Reversed string word by word:
running is goku