Thursday, September 18, 2008

Character Shifting Algorithm

Here's a simple character shifting algorithm. It gives me a chance to test out google-code-prettify JavaScript/css I just added to my blog.



public class CharacterShift {

/**
* @param args
*/
public static void main(String[] args) {
char[] word = "iphone".toCharArray();
int shift = 3;
char[] result = new char[word.length];

for (int k = 0; k < word.length; k++) {
int p = (k + shift) % word.length;

result[p] = word[k];
}

System.out.println(String.valueOf(word) + " shifted by " + shift + " becomes " + String.valueOf(result));
}

}

No comments: