Open In App

Program to reverse order of words in a sentence

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:

Below is the implementation of the algorithm:




#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;
}




#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;
}




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);
    }
}




def reverse_words(sentence):
    words = sentence.split()
    reversed_sentence = ' '.join(reversed(words))
    print(reversed_sentence)
 
 
sentence = "Programming is fun"
reverse_words(sentence)




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));
    }
}




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));

Output
fun 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.


Article Tags :