Reverse a String: flowchart, algorithm and program
This program reads a word and prints its characters in reverse order, so "hello" becomes "olleh". It finds the length of the word, then loops from the last character back to the first and prints each one.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read word.
- Step 3: Set len = strlen(word).
- Step 4: Print "Reversed: ".
- Step 5: Set i = len - 1.
- Step 6: If i >= 0, go to step 7; otherwise go to step 9.
- Step 7: Print word[i].
- Step 8: Set i = i - 1. Go to step 6.
- Step 9: Print newline.
- Step 10: Stop.
Pseudocode
BEGIN
READ word
len ← strlen(word)
PRINT "Reversed: "
i ← len - 1
WHILE i >= 0 DO
PRINT word[i]
i ← i - 1
END WHILE
PRINT newline
END
Program in C, Python and Java
Reverse a String program in C
#include <stdio.h>
#include <string.h>
int main() {
char word[100];
int i, len;
printf("Enter a word: ");
scanf("%s", word);
len = strlen(word);
printf("Reversed: ");
for (i = len - 1; i >= 0; i--) {
printf("%c", word[i]);
}
printf("\n");
return 0;
}Reverse a String program in Python
word = input("Enter a word: ")
rev = ""
for i in range(len(word) - 1, -1, -1):
rev = rev + word[i]
print("Reversed:", rev)Reverse a String program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = sc.next();
String rev = "";
for (int i = word.length() - 1; i >= 0; i--) {
rev = rev + word.charAt(i);
}
System.out.println("Reversed: " + rev);
}
}
Sample input: word = hello
Output: Enter a word: Reversed: olleh
How it works
- scanf("%s", word) reads one word into the char array word, and strlen(word) stores its length in len (5 for "hello").
- Array indexes start at 0, so the last character is at index len - 1. The loop therefore starts with i = len - 1.
- The loop runs while i >= 0 and does i-- each time, moving from the last character to the first.
- Each pass prints word[i] with printf("%c", ...). For "hello" it prints o, l, l, e, h in that order, giving "olleh".
- The original string is never changed. Only the print order is reversed, so no second array is needed.
Dry run (trace table)
| Step | Line | Box | word | len | i | Output |
|---|---|---|---|---|---|---|
| 1 | 4 | Start | ||||
| 2 | 9 | Read word | hello | Enter a word: | ||
| 3 | 11 | len = strlen(word) | hello | 5 | ||
| 4 | 12 | Print "Reversed: " | hello | 5 | Reversed: | |
| 5 | 13 | i = len - 1 | hello | 5 | 4 | |
| 6 | 13 | i >= 0? | hello | 5 | 4 | |
| 7 | 14 | Print word[i] | hello | 5 | 4 | o |
| 8 | 13 | i = i - 1 | hello | 5 | 3 | |
| 9 | 13 | i >= 0? | hello | 5 | 3 | |
| 10 | 14 | Print word[i] | hello | 5 | 3 | l |
| 11 | 13 | i = i - 1 | hello | 5 | 2 | |
| 12 | 13 | i >= 0? | hello | 5 | 2 | |
| 13 | 14 | Print word[i] | hello | 5 | 2 | l |
| 14 | 13 | i = i - 1 | hello | 5 | 1 | |
| 15 | 13 | i >= 0? | hello | 5 | 1 | |
| 16 | 14 | Print word[i] | hello | 5 | 1 | e |
| 17 | 13 | i = i - 1 | hello | 5 | 0 | |
| 18 | 13 | i >= 0? | hello | 5 | 0 | |
| 19 | 14 | Print word[i] | hello | 5 | 0 | h |
| 20 | 13 | i = i - 1 | hello | 5 | -1 |
Time and space complexity
Time: O(n) · Space: O(1). The loop runs once per character of the word (n = length) and no extra array is used for the reversal.
Common mistakes
- Starting the loop at i = len instead of len - 1. word[len] is the '\0' terminator, so you read past the last real character.
- Stopping the loop at i > 0, which skips word[0], so "hello" would print "olle".
- Using scanf("%s") and expecting a sentence. It stops at the first space, so "hello world" only reads "hello". Use fgets for full lines.
Viva questions
Q1. Why does the loop start at len - 1 and not at len?
Array indices run from 0 to len-1. word[len] is the null character '\0' that ends the string, so starting there would print an invisible character instead of the last letter.
Q2. What does strlen() return, and does it count the '\0'?
strlen returns the number of characters before the null terminator. It does not count the '\0'. For "hello" it returns 5.
Q3. Why is the loop condition i >= 0 and not i > 0?
Index 0 holds the first character, which must also be printed. With i > 0 the loop would stop early and miss the first letter of the word.
Q4. What happens if the user enters an empty string, or one with a single character?
With an empty string len is 0, so i starts at -1, the condition fails at once and nothing is printed. With one character the loop runs once and prints that character. Note that scanf with %s will not accept a truly empty line, because it waits for input.
Q5. What happens if the user types 'hello world' with a space?
scanf("%s") stops reading at whitespace, so only 'hello' is stored and reversed as 'olleh'. To read a full line you would use fgets.
Q6. What is the risk of char word[100] with scanf("%s", word)?
scanf does no bounds checking, so an input longer than 99 characters overflows the array and causes undefined behaviour. You can limit it with scanf("%99s", word) or use fgets.
Q7. Does this program actually reverse the string? What would you change to reverse it in place?
No, it only prints the characters in reverse order and word is unchanged. To reverse it in place, swap word[i] with word[len-1-i] for i from 0 to len/2 - 1 (or use two indices moving towards each other), then print the string.
Q8. What is the time complexity of this program?
O(n), where n is the length of the word. strlen walks the string once and the loop runs n times, so the total is linear. Extra space is O(1) because no second array is used.
Frequently asked questions
How do you reverse a string in C without using a library function?
Loop from index len - 1 down to 0 and print word[i] each time. Only strlen is used here, to get the length, and you could count the characters yourself with a loop instead.
Does this program actually change the string?
No. It only prints the characters in reverse order and word stays as "hello". To store the reversed string, swap characters from both ends or copy into a second array.
Why does the loop start at len - 1 and not len?
Indexes run from 0 to len - 1. Index len holds the null character '\0' that ends the string, which is not part of the word.