new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
26
Task/Caesar_cipher/Java/caesar_cipher.java
Normal file
26
Task/Caesar_cipher/Java/caesar_cipher.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
public class Cipher {
|
||||
public static void main(String[] args) {
|
||||
String enc = Cipher.encode(
|
||||
"The quick brown fox Jumped over the lazy Dog", 12);
|
||||
System.out.println(enc);
|
||||
System.out.println(Cipher.decode(enc, 12));
|
||||
}
|
||||
|
||||
public static String decode(String enc, int offset) {
|
||||
return encode(enc, -offset);
|
||||
}
|
||||
|
||||
public static String encode(String enc, int offset) {
|
||||
offset = offset % 26 + 26;
|
||||
StringBuilder encoded = new StringBuilder();
|
||||
for (char i : enc.toLowerCase().toCharArray()) {
|
||||
if (Character.isLetter(i)) {
|
||||
int j = (i - 'a' + offset) % 26;
|
||||
encoded.append((char) (j + 'a'));
|
||||
} else {
|
||||
encoded.append(i);
|
||||
}
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue