VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-reverse-a-word/

⇱ Program to reverse a word - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to reverse a word

Last Updated : 19 Jan, 2024

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:

  1. Create a variable to store the reversed word.
  2. Traverse the original word from the last character to the first.
  3. Append each character to the reversed word.

Below is the implementation of the algorithm:


Output
Original Word: Hello
Reversed Word: olleH

Time Complexity: O(N) where N is the length of the word.
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: