Вот мое решение для сдвига символов в строке.
public static void main(String[] args) {
String input = "melike";
int shiftCount = 13;
char[] encryptedChars = encryptedChars(shiftCount);
System.out.println(new String(encryptedChars));
char[] decryptedChars = decryptedChars(shiftCount);
System.out.println(new String(decryptedChars));
String encrypted = encrypt(input, shiftCount);
System.out.println(encrypted);
String decrypted = decrypt(encrypted, shiftCount);
System.out.println(decrypted);
System.out.println(input.equals(decrypted));
}
private static String decrypt(String input, int shiftCount) {
char[] decryptedChars = decryptedChars(shiftCount);
char[] chars = input.toCharArray();
char[] newChars = new char[chars.length];
for (int i = 0; i < chars.length; i++) {
int diff = chars[i] - 'a';
newChars[i] = decryptedChars[diff];
}
return new String(newChars);
}
private static String encrypt(String input, int shiftCount) {
char[] encryptedChars = encryptedChars(shiftCount);
char[] chars = input.toCharArray();
char[] newChars = new char[chars.length];
for (int i = 0; i < chars.length; i++) {
int diff = chars[i] - 'a';
newChars[i] = encryptedChars[diff];
}
return new String(newChars);
}
private static char[] encryptedChars(int shiftCount) {
char[] newChars = new char[26];
for (int i = 0; i < 26; i++) {
newChars[i] = (char) ('a' + (i + shiftCount) % 26);
}
return newChars;
}
private static char[] decryptedChars(int shiftCount) {
char[] newChars = new char[26];
for (int i = 0; i < 26; i++) {
newChars[i] = (char) ('a' + (i - shiftCount + 26) % 26);
}
return newChars;
}