C Program
#include<stdio.h> #include<string.h> int main() { char s[100]; gets(s); for(int i=strlen(s)-1; i>=0; i--) printf("%c", s[i]); }
C Output
Input: hello Output: olleh
C++ Program
#include<iostream> #include<string> using namespace std; int main() { string s; getline(cin, s); for(int i=s.size()-1; i>=0; i--) cout << s[i]; }
C++ Output
Input: world Output: dlrow
JAVA Program
import java.util.*; class R { public static void main(String[] a) { String s = new Scanner(System.in).nextLine(); for(int i=s.length()-1; i>=0; i--) System.out.print(s.charAt(i)); } }
JAVA Output
Input: Java Output: avaJ
Python Program
s = input() print(s[::-1])
Python Output
Input: Python Output: nohtyP
In-Depth Learning – Complete Concept in Paragraphs
What Does "Reversing a String" Mean?
Reversing a string means flipping the characters so that the first character becomes the last, and the last becomes the first. For example, the reverse of "code" is "edoc". This is one of the simplest and most commonly asked programming tasks, and it tests your understanding of loops, string indexing, and basic data handling in various programming languages.
How the Code Works
In C/C++, we loop from the end index of the string (length - 1) to index 0 by using a for loop and print each character individually.
In Java, we utilize charAt(i) within a reverse loop.
Python provides the most shorthand method: s[::-1]. This is string slicing, with [::-1] informing Python to take the whole string but in a reversed step.
Example
Input: "coding"
Process:
Original index: c o d i n g → Index 0 to 5
Reversed index: g n i d o c → Printed from 5 to 0
Output: "gnidoc"
This reverse method is non-destructive (i.e., the original string is not changed unless it is stored in reverse).
Real-Life Analogy
Imagine reading a word in a mirror. If you type "MIRROR" and read it in a mirror, it looks reversed: "RORRIM". Reversing a string is analogous to reading it through that mirror.
Second analogy — reading names on sport jerseys via a rear-view camera!
Where and When Is It Used?
Reversing a string finds most of its uses in:
Palindrome checks (checking a string against its reverse)
Text processing (e.g., word reversal, reading messages back)
Interview questions (simple demonstration of logic)
Practice recursion (reverse a string recursively)
Competitive programming
Also used in encryption, compression, or pretty-printing output displays.
Time and Space Complexity
Time Complexity: O(n) → you have to go through each character once
Space Complexity:
O(1) if done in-place (such as printing in reverse)
O(n) if kept in a fresh variable (as in slicing or constructing a reversed string)
Python's slicing is secretly making a copy, but it's fast and memory efficient.
Python-Specific Tip
Python's slicing is very, very powerful:
python
s[::-1] # reverses the entire string
s[::2] # selects every second char
s[1::2] # skips first, then every second
Mastering this unlocks powerful text-processing powers with minimal code.
SEO-Optimized Natural Paragraph for Ranking
Need to reverse a string in C, C++, Java, or Python? This tutorial provides the shortest, most efficient code examples for reversing a string with and without loops. String reversal is among the fundamental programming problems and finds extensive use in text processing, palindrome checking, encryption, and competitive programming. Find out how to reverse a string with basic loops in Java/C/C++ and how Python just makes it easy with slicing. This tutorial contains live examples, analogies, and performance-enhanced code for mastering this basic string operation.
Social Plugin