Program to reverse a word Last Updated : 19 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Write a program to reverse a given word. Examples: Input: "Hello" Output: "olleH" Input: "Programming" Output: "gnimmargorP" Approach: To solve the problem, follow the below idea: It can be observed that we can reverse a word by traversing over the original string 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 word.Traverse the original word from the last character to the first.Append each character to the reversed word.Below is the implementation of the algorithm: C++ #include <bits/stdc++.h>; using namespace std; string reverseWord(string word) { string reversedWord = ""; for(int i = word.length() - 1; i >= 0; i--) { reversedWord.push_back(word[i]); } return reversedWord; } int main() { string word = "Hello"; cout<< "Original Word: " << word << endl; cout << "Reversed Word: " << reverseWord(word) << endl; return 0; } C #include <stdio.h> // Function to reverse a word and return the reversed word char* reverseWord(const char* word) { int length = strlen(word); // +1 for null terminator char* reversedWord = (char*)malloc((length + 1) * sizeof(char)); for (int i = 0; i < length; i++) { reversedWord[i] = word[length - 1 - i]; } // Null-terminate the reversed string reversedWord[length] = '\0'; return reversedWord; } int main() { const char* originalWord = "Hello"; char* reversed = reverseWord(originalWord); printf("Original Word: %s\n", originalWord); printf("Reversed Word: %s\n", reversed); free(reversed); // Don't forget to free the allocated // memory return 0; } Java public class ReverseWord { // Function to reverse a word and return the reversed // word static String reverseWord(String word) { int length = word.length(); StringBuilder reversedWord = new StringBuilder(length); for (int i = length - 1; i >= 0; i--) { reversedWord.append(word.charAt(i)); } return reversedWord.toString(); } public static void main(String[] args) { String originalWord = "Hello"; String reversed = reverseWord(originalWord); System.out.println("Original Word: " + originalWord); System.out.println("Reversed Word: " + reversed); } } Python3 def reverse_word(word): return word[::-1] def main(): original_word = "Hello" reversed_word = reverse_word(original_word) print("Original Word:", original_word) print("Reversed Word:", reversed_word) if __name__ == "__main__": main() C# using System; class Program { // Function to reverse a word and return the reversed // word static string ReverseWord(string word) { char[] charArray = word.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } static void Main() { string originalWord = "Hello"; string reversedWord = ReverseWord(originalWord); Console.WriteLine("Original Word: " + originalWord); Console.WriteLine("Reversed Word: " + reversedWord); } } JavaScript function reverseWord(word) { return word.split('').reverse().join(''); } function main() { const originalWord = "Hello"; const reversedWord = reverseWord(originalWord); console.log("Original Word:", originalWord); console.log("Reversed Word:", reversedWord); } main(); OutputOriginal Word: Hello Reversed Word: olleH Time Complexity: O(N) where N is the length of the word.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Program to reverse a word C code_r Follow Improve Article Tags : DSA Similar Reads 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 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 words in a string Given a string str, your task is to reverse the order of the words in the given string. Note that str may contain leading or trailing dots(.) or multiple trailing dots(.) between two words. The returned string should only have a single dot(.) separating the words.Examples:Input: str = "i.like.this.p 11 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 Write a program to reverse digits of a number Given an Integer n, find the reverse of its digits.Examples: Input: n = 122Output: 221Explanation: By reversing the digits of number, number will change into 221.Input: n = 200Output: 2Explanation: By reversing the digits of number, number will change into 2.Input: n = 12345 Output: 54321Explanation 8 min read Like