Program to reverse a word
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(); |
Output
Original Word: Hello Reversed Word: olleH
Time Complexity: O(N) where N is the length of the word.
Auxiliary Space: O(N)
Contact Us