Knowee
Questions
Features
Study Tools

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.

🧐 Not the exact question you are looking for?Go ask a question

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.

This problem has been solved

Similar Questions

Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.Ex: If the input is:Hello thereHeydonethe output is:ereht olleHyeH

Write c function to reverse words in sentence (without using external arrays)

In a given sentence or an array of characters, reverse the order of words.

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.Sample InputwordSample OutputdrowD

Word Reversal in a StringDesign a Python program to accept a string from user.The string can be composed of words separated by a spaces. Implement the code logic to modify and return the given string ‘str’ such that its every word is inverted but appears in the same order as the original string.Constraints:NAExample:Sample Input:Python StringsSample Output:nohtyP sgnirtS

1/3

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.