Skip to content

Instantly share code, notes, and snippets.

@dideler
Created February 17, 2013 01:22
Show Gist options
  • Save dideler/4969581 to your computer and use it in GitHub Desktop.
Save dideler/4969581 to your computer and use it in GitHub Desktop.
Flip Text - flips a line of text so that it appears to be upside-down and mirrored
/**
* Copyright 2009 Dennis Ideler
*
* $ javac flipText.java
* $ java flipText
* hello world, i'm dennis
* sıuuǝp ɯ,ı 'p1ɹoʍ o11ǝɥ
*/
import java.io.*;
public class flipText
{
flipText()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = "";
try { text = br.readLine(); }
catch(Exception e) { e.printStackTrace(); }
String result = flipString(text);
System.out.println(result);
}
private String flipString(String text)
{
text = text.toLowerCase();
String result = "";
for (int i = text.length() - 1; i >= 0; --i)
{
// Flip and mirror the text.
result += flipChar(text.charAt(i));
}
return result;
}
private char flipChar(char c)
{
switch (c)
{
case 'a': return '\u0250';
case 'b': return 'q';
case 'c': return '\u0254';
case 'd': return 'p';
case 'e': return '\u01DD';
case 'f': return '\u025F';
case 'g': return 'b';
case 'h': return '\u0265';
case 'i': return '\u0131'; // or \u0323
case 'j': return '\u0638';
case 'k': return '\u029E';
case 'l': return '1';
case 'm': return '\u026F';
case 'n': return 'u';
case 'o': return 'o';
case 'p': return 'd';
case 'q': return 'b';
case 'r': return '\u0279';
case 's': return 's';
case 't': return '\u0287';
case 'u': return 'n';
case 'v': return '\u028C';
case 'w': return '\u028D';
case 'x': return 'x';
case 'y': return '\u028E';
case 'z': return 'z';
case '[': return ']';
case ']': return '[';
case '(': return ')';
case ')': return '(';
case '{': return '}';
case '}': return '{';
case '?': return '\u00BF';
case '\u00BF': return '?';
case '!': return '\u00A1';
case '\'': return ',';
case ',': return '\'';
default: return c;
}
}
public static void main(String[] args) { new flipText(); }
}
@danisherror
Copy link

danisherror commented May 26, 2025

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class FlipText {

    public static void main(String[] args) {
        FlipText flipper = new FlipText();
        flipper.run();
    }

    public void run() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            String input;

            System.out.println("Enter text to flip (type 'exit' to quit):");

            while ((input = br.readLine()) != null) {
                if (input.equalsIgnoreCase("exit")) {
                    break;
                }

                String flipped = flipString(input);
                System.out.println("Flipped: " + flipped);
                System.out.println();
            }

        } catch (IOException e) {
            System.err.println("Error reading input:");
            e.printStackTrace();
        }
    }

    private String flipString(String text) {
        text = text.toLowerCase();
        StringBuilder result = new StringBuilder();

        for (int i = text.length() - 1; i >= 0; i--) {
            result.append(flipChar(text.charAt(i)));
        }

        return result.toString();
    }

    private char flipChar(char c) {
        switch (c) {
            case 'a': return '\u0250';
            case 'b': return 'q';
            case 'c': return '\u0254';
            case 'd': return 'p';
            case 'e': return '\u01DD';
            case 'f': return '\u025F';
            case 'g': return 'b';
            case 'h': return '\u0265';
            case 'i': return '\u0131';
            case 'j': return '\u0638';
            case 'k': return '\u029E';
            case 'l': return '1';
            case 'm': return '\u026F';
            case 'n': return 'u';
            case 'o': return 'o';
            case 'p': return 'd';
            case 'q': return 'b';
            case 'r': return '\u0279';
            case 's': return 's';
            case 't': return '\u0287';
            case 'u': return 'n';
            case 'v': return '\u028C';
            case 'w': return '\u028D';
            case 'x': return 'x';
            case 'y': return '\u028E';
            case 'z': return 'z';
            case '[': return ']';
            case ']': return '[';
            case '(': return ')';
            case ')': return '(';
            case '{': return '}';
            case '}': return '{';
            case '?': return '\u00BF';
            case '\u00BF': return '?';
            case '!': return '\u00A1';
            case '\'': return ',';
            case ',': return '\'';
            default: return c;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment