KamaSutra.java


Below is the syntax highlighted version of KamaSutra.java from §3.1 Using Data Types.


/*************************************************************************
 *  Compilation:  javac KamaSutra.java
 *  Execution:    java KamaSutra word
 *
 *  Encode a message using the Kama Sutra cipher.
 *  
 *  % java KamaSutra "MEET AT ELEVEN"
 *  SUUR QR UWUPUO
 * 
 *  % java KamaSutra "SUUR QR UWUPUO"
 *  MEET AT ELEVEN
 *
 *************************************************************************/

public class KamaSutra {

    public static void main(String[] args) {
        String top = "ABCEFGHKLMNPR";
        String bot = "QDZUJIXYWSOVT";
        String s = args[0].toUpperCase();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if      (top.indexOf(c) >= 0) System.out.print(bot.charAt(top.indexOf(c)));
            else if (bot.indexOf(c) >= 0) System.out.print(top.charAt(bot.indexOf(c)));
            else                          System.out.print(c);
        }
        System.out.println();
    }

}


Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:07:43 EST 2011.