1. Home
  2. Computing & Technology
  3. Java
Paul Leahy
Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Monday's Programming Question

Monday September 29, 2008

Ever thought of becoming a spy? No, me neither. Still, I always liked the part about passing secret messages from one person to another. This week you will need to write a decoding program to be able to answer my question.

I'm looking for a simple solution that takes a line of encoded text entered by the user and uses a combination of a normal for loop and switch statement to decode it. Finally, display the decoded text to the user.

The code is a simple letter switch:

Decodedabcdefghijklm
Encodedprjitgcoxbsyn
Decodednopqr stuvwx yz
Encodedwdemh uvlzfq ka
For example, the name "bob" becomes "rdr" (i.e., b=r, o=d, b=r). Don't worry about capital letters, just assume that all of the text is in lowercase.

The question is can your program decode this first line from a book by Iain Banks?

xv fpu vot ipk nk chpwindvoth tqeyditi

Comments
September 29, 2008 at 11:13 am
(1) Jose Ayerdis says:

For example, the name “bob” becomes “joj” (i.e., b=j, o=h, b=j). Don’t worry about capital letters, just assume that all of the text is in lowercase.

BOB becomes JHJ am i right??

September 30, 2008 at 12:15 am
(2) Paul Leahy says:

hey Jose, good spot – unfortunately it turns out that I put the coded and encoded letters around the wrong way *embarrassed cough* – “bob” does in fact become “rdr”.

September 30, 2008 at 6:15 am
(3) Lennie says:

Here’s the C++ version of the puzzle:

http://codepad.org/NrdY3fHZ

September 30, 2008 at 1:15 pm
(4) james curtis says:

/**
*
* @author james
* 9/29/2009
*/
public class Decode {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

String ar[]= {”x”,”v”,” “,”f”,”p”,”u”,” “,”v”,”o”,”t”,” “,”i”,”p”,”k”,” “,”n”,”k”,” “,”c”,”h”,”p”,”w”,”i”,”n”,”d”,”v”,”o”,”t”,”h”,” “,”t”,”q”,”e”,”y”,”d”,”i”,”t”,”i”};
String Encoded[] = {”p”,”r”,”j”,”i”,”t”,”g”,”c”,”o”,”x”,”b”,”s”,”y”,”n”,”w”,”d”,”e”,”m”,”h”,”u”,”v”,”l”,”z”,”f”,”q”,”k”,”a”,” “};
String Decoded[] = {”a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”,”i”,”j”,”k”,”l”,”m”,”n”,”o”,”p”,”q”,”r”,”s”,”t”,”u”,”v”,”w”,”x”,”y”,”z”,” “};
int a=0;

String Decode[] = new String[ar.length];
while(a

September 30, 2008 at 1:18 pm
(5) James Curtis says:

the code is in java

October 1, 2008 at 5:25 am
(6) Oktay says:

public class Decoding {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String text = new String(”xv fpu vot ipk nk chpwindvoth tqeyditi.”);

char array[];
array = text.toCharArray();
//System.out.println(array.length + ” ” + array[0]);

for (int i = 0; i

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore Java
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Java

©2009 About.com, a part of The New York Times Company.

All rights reserved.