Program to reverse a sentence Last Updated : 19 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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 sentence in reverse order and then pushing the characters from the back to a new string. Finally, return the final string. Step-by-step algorithm: Create a variable to store the reversed sentence.Traverse the original sentence from the last character to the first.Append each character to the reversed sentence.Below is the implementation of the algorithm: C++ #include <iostream> using namespace std; string reverseSentence(string sentence) { string reversedString = ""; for (int i = sentence.length() - 1; i >= 0; i--) { reversedString.push_back(sentence[i]); } return reversedString; } int main() { string sentence = "Practice with GFG"; cout << reverseSentence(sentence) << endl; return 0; } C #include <stdio.h> #include <string.h> char* reverseSentence(const char* sentence) { int length = strlen(sentence); char* reversedString = (char*)malloc((length + 1) * sizeof(char)); for (int i = length - 1, j = 0; i >= 0; i--, j++) { reversedString[j] = sentence[i]; } reversedString[length] = '\0'; return reversedString; } int main() { const char* sentence = "Practice with GFG"; char* reversed = reverseSentence(sentence); printf("%s\n", reversed); free(reversed); // Don't forget to free the allocated memory return 0; } Java public class ReverseSentence { public static String reverseSentence(String sentence) { StringBuilder reversedString = new StringBuilder(); for (int i = sentence.length() - 1; i >= 0; i--) { reversedString.append(sentence.charAt(i)); } return reversedString.toString(); } public static void main(String[] args) { String sentence = "Practice with GFG"; System.out.println(reverseSentence(sentence)); } } Python3 def reverse_sentence(sentence): reversed_string = "" for i in range(len(sentence) - 1, -1, -1): reversed_string += sentence[i] return reversed_string if __name__ == "__main__": sentence = "Practice with GFG" print(reverse_sentence(sentence)) C# using System; class Program { static string ReverseSentence(string sentence) { char[] charArray = sentence.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } static void Main() { string sentence = "Practice with GFG"; Console.WriteLine(ReverseSentence(sentence)); } } JavaScript function reverseSentence(sentence) { return sentence.split('').reverse().join(''); } const sentence = "Practice with GFG"; console.log(reverseSentence(sentence)); OutputGFG htiw ecitcarP Time Complexity: O(N), where N is the length of the sentence.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Program to reverse a sentence C code_r Follow Improve Article Tags : DSA Similar Reads 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: 3 min read Reverse a String | Shell Programming In shell scripting, reversing a string can be done using various methods, such as rev, awk, sed, and Perl. Here, we provide examples of reversing a string using different approaches and commands in Unix/Linux systems. We are given a string and we have to use shell script to print it in the reverse o 4 min read How to reverse a string in R Reversing a string means changing its order so that the last character becomes the first, the second last character becomes the second, and so on. A while loop is a control flow statement used in R programming to execute a block of code repeatedly as long as a specified condition is true. By using a 4 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 How to reverse a String in Python Reversing a string is a common task in Python, which can be done by several methods. In this article, we discuss different approaches to reversing a string. One of the simplest and most efficient ways is by using slicing. Letâs see how it works:Using string slicingThis slicing method is one of the s 4 min read Like