CBSE Class 11 Computer Science Question 21 of 21

String Manipulation — Question 15

Back to all questions
15
Question

Question 15

Write a program to input a line of text and create a new line of text where each word of input line is reversed.

Solution
str = input("Enter a string: ")
words = str.split()
newStr = ""

for w in words :
    rw = ""
    for ch in w :
        rw = ch + rw
    newStr += rw + " "

print(newStr)
Output
Enter a string: Python is Fun
nohtyP si nuF
Answer

str
=
input
(
"Enter a string: "
)
words
=
str
.
split
()
newStr
=
""
for
w
in
words
:
rw
=
""
for
ch
in
w
:
rw
=
ch
+
rw
newStr
+=
rw
+
" "
print
(
newStr
)
Output
Enter a string: Python is Fun
nohtyP si nuF