Thursday 13 August 2020

Reverse internal content of every second word

 Sample Input: one two three four five six seven eight

Output: one owt three ruof five xis seven thgie

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
string_data=input("Enter a string: ")
list1=string_data.split()
print(list1)
list2=[]
i=0
while i<len(list1):
	if i%2==0:
		list2.append(list1[i])
	else:
		list2.append(list1[i][::-1])# Reversing word and then adding to list2
	i=i+1
print(list2)
result=" ".join(list2)
print("The string you entered:", string_data, sep="\n")
print("The result after reversing order of words in the string: ", result, sep="\n")

No comments:

Post a Comment