-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCeaser_Cipher.java
More file actions
35 lines (23 loc) · 869 Bytes
/
Copy pathCeaser_Cipher.java
File metadata and controls
35 lines (23 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import org.jetbrains.annotations.NotNull;
import java.util.*;
public class Ceaser_Cipher {
public String applyCaesar(@NotNull String text, int shift)
{
char[] chars = text.toCharArray();
for (int i=0; i < text.length(); i++)
{
char c = chars[i];
if (c >= 32 && c <= 127)
{
// Change base to make life easier, and use an
// int explicitly to avoid worrying... cast later
int x = c - 32;
x = (x + shift) % 96;
if (x < 0)
x += 96; //java modulo can lead to negative values!
chars[i] = (char) (x + 32);
}
}
return new String(chars);
}
}