KamaSutra.java


This is the syntax highlighted version of KamaSutra.java from 3.1 Using Data Types of
Introduction to Computer Science by Robert Sedgewick and Kevin Wayne.

/*************************************************************************
 *  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();
    }

}


Last updated: Fri Jul 16 15:45:57 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.