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

  1. Step 1: Start.
  2. Step 2: Read word.
  3. Step 3: Set len = strlen(word).
  4. Step 4: Print "Reversed: ".
  5. Step 5: Set i = len - 1.
  6. Step 6: If i >= 0, go to step 7; otherwise go to step 9.
  7. Step 7: Print word[i].
  8. Step 8: Set i = i - 1. Go to step 6.
  9. Step 9: Print newline.
  10. 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)

StepLineBoxwordleniOutput
14Start
29Read wordhelloEnter a word:
311len = strlen(word)hello5
412Print "Reversed: "hello5Reversed:
513i = len - 1hello54
613i >= 0?hello54
714Print word[i]hello54o
813i = i - 1hello53
913i >= 0?hello53
1014Print word[i]hello53l
1113i = i - 1hello52
1213i >= 0?hello52
1314Print word[i]hello52l
1413i = i - 1hello51
1513i >= 0?hello51
1614Print word[i]hello51e
1713i = i - 1hello50
1813i >= 0?hello50
1914Print word[i]hello50h
2013i = i - 1hello5-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.

More programs

← All programs

Try AI Flowchart free →