ROT13 Encoder/Decoder - Problem
Implement a ROT13 encoder/decoder function that transforms alphabetic characters by shifting them 13 positions in the alphabet. ROT13 is a simple letter substitution cipher that replaces each letter with the letter 13 positions after it in the alphabet.
Key Rules:
- Only rotate alphabetic characters (a-z, A-Z)
- Preserve case: uppercase letters stay uppercase, lowercase stay lowercase
- Keep all non-alphabetic characters unchanged (numbers, spaces, punctuation)
- ROT13 is its own inverse: applying it twice returns the original string
For example: 'A' becomes 'N', 'B' becomes 'O', ..., 'N' becomes 'A', 'Z' becomes 'M'
Input & Output
Example 1 — Basic Mixed Case
$
Input:
text = "Hello World!"
›
Output:
"Uryyb Jbeyq!"
💡 Note:
H→U, e→r, l→y, l→y, o→b, space stays, W→J, o→b, r→e, l→y, d→q, ! stays unchanged
Example 2 — Numbers and Punctuation
$
Input:
text = "Test123!@#"
›
Output:
"Grfg123!@#"
💡 Note:
Only alphabetic characters are rotated: T→G, e→r, s→f, t→g. Numbers and symbols remain unchanged
Example 3 — Wraparound Case
$
Input:
text = "XYZ abc"
›
Output:
"KLM nop"
💡 Note:
Shows alphabet wraparound: X→K, Y→L, Z→M, a→n, b→o, c→p
Constraints
- 1 ≤ text.length ≤ 1000
- text contains ASCII characters only
- Mix of uppercase, lowercase, numbers, and special characters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code