Program to reverse order of words in a sentence Last Updated : 18 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Write a program to reverse the order of words in a given sentence. A word is defined as a sequence of non-space characters. The sentence is a collection of words separated by spaces. Examples: Input: "Hello World"Output: "World Hello" Input: "Programming is fun"Output: "fun is Programming" Approach: To solve the problem, follow the below idea: The problem can be solved by splitting the sentence into words. After splitting the sentence into words, reverse the order of the words and then reconstruct the sentence by appending all the words with a space between them. Step-by-step algorithm:Split the sentence into words using space as the delimiter.Reverse the order of the words.Reconstruct the sentence by joining the reversed words with spaces.Below is the implementation of the algorithm: C++ #include <bits/stdc++.h> using namespace std; string reverseWords(string sentence) { // vector to store the words vector<string> words; string word = "", reversedSentence = ""; for (int i = 0; i < sentence.length(); i++) { if (sentence[i] == ' ') { words.push_back(word); word = ""; } else { word += sentence[i]; } } if (word != "") { words.push_back(word); } // Append the words in reverse order for (int i = words.size() - 1; i >= 0; i--) { reversedSentence.append(words[i]); reversedSentence.append(" "); } return reversedSentence; } int main() { char sentence[] = "Programming is fun"; cout << reverseWords(sentence); return 0; } C #include <stdio.h> #include <string.h> void reverseWords(char* sentence) { char* word = strtok(sentence, " "); char* words[100]; int count = 0; while (word != NULL) { words[count++] = word; word = strtok(NULL, " "); } for (int i = count - 1; i >= 0; --i) { printf("%s ", words[i]); } } int main() { char sentence[] = "Programming is fun"; reverseWords(sentence); return 0; } Java public class ReverseWords { public static void reverseWords(String sentence) { String[] words = sentence.split(" "); for (int i = words.length - 1; i >= 0; i--) { System.out.print(words[i] + " "); } } public static void main(String[] args) { String sentence = "Programming is fun"; reverseWords(sentence); } } Python3 def reverse_words(sentence): words = sentence.split() reversed_sentence = ' '.join(reversed(words)) print(reversed_sentence) sentence = "Programming is fun" reverse_words(sentence) C# using System; class Program { static void Main() { string sentence = "Programming is fun"; ReverseWords(sentence); } static void ReverseWords(string sentence) { string[] words = sentence.Split(' '); Array.Reverse(words); Console.WriteLine(string.Join(" ", words)); } } JavaScript function reverseWords(sentence) { // Split the sentence into an array of words const words = sentence.split(' '); // Reverse the order of the words const reversedWords = words.reverse(); // Join the reversed words to form the reversed sentence const reversedSentence = reversedWords.join(' '); return reversedSentence; } const sentence = "Programming is fun"; console.log(reverseWords(sentence)); Outputfun is Programming Time Complexity: O(N), where N is the length of the sentence.Auxiliary Space: O(N), additional space is used for storing words. Comment More infoAdvertise with us Next Article Program to reverse order of words in a sentence C code_r Follow Improve Article Tags : DSA Similar Reads Print words of a string in reverse order Let there be a string say "I AM A GEEK". So, the output should be "GEEK A AM I" . This can done in many ways. One of the solutions is given in Reverse words in a string . Examples: Input : I AM A GEEK Output : GEEK A AM I Input : GfG IS THE BEST Output : BEST THE IS GfG This can be done in more simp 10 min read Program to reverse a sentence Write a program to reverse a sentence. Examples: Input: "Practice with GFG" Output: "GFG htiw ecitcarP" Input: "Programming is fun" Output: "nuf si gnimmargorP" Approach: To solve the problem, follow the below idea: It can be observed that we can reverse a sentence by traversing over the original se 2 min read Javascript Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i"Examples:Â Input: s = "geeks quiz practice code"Â Output: s = "code practice quiz geeks"Input: s = "getting good at coding needs a lot of practice"Â Output: s = "pra 4 min read Python - Sort words of sentence in ascending order Sorting words in a sentence in ascending order can be useful for tasks like text analysis, data preprocessing, or even fun applications like creating word puzzles. Itâs simple to achieve this using Python. In this article, we will explore different methods to do this.Using sorted() with SplitThe mos 3 min read Reverse each word in a sentence in Python In this article, we will explore various methods to reverse each word in a sentence. The simplest approach is by using a loop.Using LoopsWe can simply use a loop (for loop) to reverse each word in a sentence.Pythons = "Hello World" # Split 's' into words words = s.split() # Reverse each word using a 2 min read Like