Reverse Text

Reverse strings character by character or reverse each line separately while preserving line structure.

Back to all tools on ToolForge

More in Text Tools

Input



Result

About Reverse Text

This reverse text tool flips strings character by character using JavaScript's native string methods. It supports two modes: reversing the entire text as one string (including newlines) or reversing each line individually while preserving line structure. The tool handles Unicode characters including emoji and international text.

It is useful for palindrome checking, string manipulation exercises, programming interviews, cryptography puzzles, data obfuscation experiments, testing text processing functions, creating mirror text effects, debugging string algorithms, and educational demonstrations of reversal operations.

String Reversal Algorithm

Text reversal works by flipping character order:

Reversal Process:

Input: "Hello, World!"

Step-by-step:
  Position: 0  1  2  3  4  5  6  7  8  9  10 11 12
  Original: H  e  l  l  o  ,     W  o  r  l  d  !
  Reversed: !  d  l  r  o  W     ,  o  l  l  e  H

Result: "!dlroW ,olleH"

JavaScript Implementation:
  function reverse(str) {
    return str.split("").reverse().join("");
  }

  // Or using spread operator for Unicode:
  function reverseUnicode(str) {
    return [...str].reverse().join("");
  }

Time Complexity: O(n) where n = string length
Space Complexity: O(n) for the new string

Line-by-Line Reversal:
  Input:
    Line 1
    Line 2

  Output:
    1 eniL
    2 eniL

  (Line breaks preserved, each line reversed separately)

Reversal Examples

Example 1: Simple Word
  Input:  "hello"
  Output: "olleh"

Example 2: Sentence
  Input:  "The quick brown fox"
  Output: "xof nworb kciuq ehT"

Example 3: With Punctuation
  Input:  "Hello, World!"
  Output: "!dlroW ,olleH"

Example 4: Numbers
  Input:  "12345"
  Output: "54321"

Example 5: Mixed Content
  Input:  "abc123XYZ"
  Output: "ZYX321cba"

Example 6: Palindrome Test
  Input:  "racecar"
  Output: "racecar" (same as input - it's a palindrome!)

Example 7: Multi-line (Entire Text)
  Input:
    Hello
    World

  Output:
    dlroW
    olleH
  (Note: newline position also reversed)

Example 8: Multi-line (Each Line)
  Input:
    Hello
    World

  Output:
    olleH
    dlroW
  (Line structure preserved)

Example 9: Unicode Characters
  Input:  "Hello δΈ–η•Œ"
  Output: "η•ŒδΈ– olleH"

Example 10: Emoji
  Input:  "Hi πŸ˜€"
  Output: "πŸ˜€ iH"

Palindrome Reference

Type Example Reversed
Single Words racecar, level, madam, rotor (unchanged)
Numbers 12321, 1001, 1331 (unchanged)
Phrases (ignoring spaces) A man a plan a canal Panama (unchanged when normalized)
Names Anna, Bob, Eve, Hannah (unchanged, case-insensitive)
Long Palindromes Was it a car or a cat I saw (unchanged when normalized)

Use Cases in Programming

1. Interview Questions:
   - "Reverse a string without built-in methods"
   - "Check if a string is a palindrome"
   - "Reverse words in a sentence"

2. Algorithm Testing:
   - Verify string manipulation functions
   - Test Unicode handling in text processing
   - Benchmark string operation performance

3. Data Processing:
   - Decode reversed data formats
   - Process mirrored text in images (OCR)
   - Handle right-to-left text conversion

4. Cryptography Education:
   - Demonstrate simple transposition ciphers
   - Teach encoding/decoding concepts
   - Create puzzle challenges

5. Debugging:
   - Inspect string structure visually
   - Find hidden patterns in data
   - Verify text transformation pipelines

6. Fun/Creative:
   - Generate mirror text for design
   - Create puzzle game content
   - Write palindromic poetry/code

Reversal in Different Languages

Language Code Example
JavaScript str.split("").reverse().join("")
Python string[::-1]
Java new StringBuilder(str).reverse().toString()
C# new string(str.Reverse().ToArray())
PHP strrev($str)
Ruby str.reverse
Go (no built-in; use rune slice)
Rust s.chars().rev().collect()

Unicode Considerations

Unicode Character Types:

1. Basic Multilingual Plane (BMP):
   - Most common characters
   - Single 16-bit code unit
   - Reverses correctly with simple methods

2. Supplementary Characters:
   - Emoji, rare CJK characters
   - Use two 16-bit code units (surrogate pairs)
   - May break with naive reversal

3. Combining Characters:
   - Accents, diacritics (é = e + ́)
   - Should stay attached to base character
   - Complex reversal needed

4. Emoji Modifiers:
   - Skin tones: πŸ‘‹πŸΏ πŸ‘‹πŸΎ πŸ‘‹πŸ½ πŸ‘‹πŸΌ πŸ‘‹πŸ»
   - Gender: πŸ‘¨β€βš•οΈ πŸ‘©β€βš•οΈ (ZWJ sequences)
   - Flags: πŸ‡ΊπŸ‡Έ πŸ‡¬πŸ‡§ πŸ‡―πŸ‡΅ (regional indicators)
   - Require grapheme-aware reversal

JavaScript Handling:
  // Simple reversal (may break emoji):
  "Hello πŸ˜€".split("").reverse().join("")

  // Better for Unicode:
  [..."Hello πŸ˜€"].reverse().join("")

  // Best: use a library like grapheme-splitter
  // for complex emoji sequences

Historical Context

Limitations

Frequently Asked Questions

What is string reversal and how does it work?
String reversal flips the order of characters in a text string. The last character becomes first, the second-to-last becomes second, and so on. For example, 'hello' reversed is 'olleh'. The algorithm iterates through the string from end to start, building a new string with characters in reverse order.
What is the difference between reversing entire text vs each line?
Reversing entire text treats all content as one string, including newline characters. 'Hello\nWorld' becomes 'dlroW\nolleH'. Reversing each line keeps line structure intact, reversing characters within each line separately. 'Hello\nWorld' becomes 'olleH\ndlroW'. Choose based on whether you need to preserve line breaks.
How does Unicode affect string reversal?
Unicode characters like emoji (πŸ˜€), accented letters (Γ©), and CJK characters (δΈ­ζ–‡) can be multi-byte. Simple reversal may break these characters. Modern JavaScript handles most Unicode correctly with proper string methods, but complex emoji with modifiers (skin tones, flags) may still cause issues. This tool uses JavaScript's native string handling for best Unicode support.
What are common use cases for text reversal?
Common uses include: palindrome checking, string manipulation in programming interviews, cryptography and encoding puzzles, data obfuscation, testing string handling functions, creating mirror text effects, debugging text processing code, and educational demonstrations of algorithms and data structures.
What is a palindrome?
A palindrome is a word, phrase, or number that reads the same forwards and backwards. Examples: 'racecar', 'level', 'madam', '12321', 'A man a plan a canal Panama' (ignoring spaces and case). Palindromes are used in recreational mathematics, linguistics, and as programming exercise examples.
Can reversed text be used for encoding?
Reversal is a simple transposition cipher but provides no real security. It's easily decoded by reversing again. Historically used in puzzles and games, not serious encryption. For actual data protection, use proper encryption algorithms like AES. Reversal is mainly useful for obfuscation, puzzles, and educational purposes.