Sunday, 15 September 2013

How to read the input and replace the character as required?

How to read the input and replace the character as required?

The code below shows a part of my program. I am trying to create an atbash
encryption for a small challenge (atbash is an encryption method; example,
I type in 'hello' and the program will output 'svool'. It reverses the
letter order. More here: http://en.wikipedia.org/wiki/Atbash)
The program works...sort-of, when i type 'abcdef' the program outputs
'zyxwvu'. However, say i wanted to type an actual word. The program won't
cycle through the array and will just output "test" (which it's NOT
supposed to do). How do I fix this?
char letter1 [] = new char []
{'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'};
char letter2 [] = new char []
{'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'};
for (int i = 0 ; i < input.length() ; i++)
{
if(i == input.length()) {
break;
}
if (input.charAt(i) == letter1[i]) {
input.setCharAt(i, letter2[i]);
System.out.println (input);
}
else if (input.charAt(i) != letter1[i]){
System.out.println ("test");
}
}

No comments:

Post a Comment