Shift character in Alphabet
25,235
It is because next chars after 'z' is punctuation chars and so on. You can shift so that 'z' will be 'n' for example.
toEncode[i] = (toEncode[i] + 13 - (int)'a') % 26 + (int)'a';
Author by
Admin
Updated on December 27, 2020Comments
-
Admin over 3 years
Possible Duplicate:
ROT-13 function in java?I have to shift all char from a string 13 places in the alphabet
private static String encode(String line) { char[] toEncode = line.toCharArray(); for (int i = 0; i < toEncode.length; i++) { if (Character.isLetter(toEncode[i])) { toEncode[i] += 13; } } line = String.valueOf(toEncode); return line; }
The Problem is that for example 'z' get to a ?. How can I solve that?
Thx for help.
-
Jayan about 12 years@ Chandra Sekhar : Thanks for the answer: Please add explanations and guiding instructions for 'homework' questions .
-
cottonman over 7 yearsI know that this thread has been on here for quite some time now but I thought it would be worth posting this anyway for future reference. I think that you have a typo in your formula. Should it not be ...% 26... instead of 25 ? Since we want 'z' to shift to 'a' and not to 'b'.
-
pepyakin over 7 yearsOh, yes, you are right. I'm mistakingly assumed that is alphabet consists of 25 letters.