Design a program such that it converts a sentence into wordlist. Reverse the wordlist then. Write the code and output for the same.
Question
Design a program such that it converts a sentence into wordlist. Reverse the wordlist then. Write the code and output for the same.
Solution
Sure, here is a Python program that converts a sentence into a word list, reverses the word list, and then prints the output.
# Step 1: Get the sentence
sentence = "This is a sample sentence"
# Step 2: Convert the sentence into a word list
word_list = sentence.split()
# Step 3: Reverse the word list
reversed_word_list = word_list[::-1]
# Step 4: Print the output
print("Original Word List: ", word_list)
print("Reversed Word List: ", reversed_word_list)
When you run this program with the sentence "This is a sample sentence", the output will be:
Original Word List: ['This', 'is', 'a', 'sample', 'sentence']
Reversed Word List: ['sentence', 'sample', 'a', 'is', 'This']
This program works by using the split()
function to divide the sentence into a list of words, and then uses slicing ([::-1]
) to reverse the order of the list.
Similar Questions
Write a program in C to print a string in reverse using a character pointer.Also print the uppercase of the first letter of the reversed word.
Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.
Which among the following methods is used to reverse the sequence of characters in a StringBuilder object?a)backwards()b)invert()c)reverse()d)flip()
Select two words, one from each set of brackets, that can be joined together to form a new word.(pin, pine, up) (print, prick, sick)
(i) Without using string slicing operations, create a function which takes in a string, reverse(string), and returns a new string in the reverse order.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.