-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCaesar.java
More file actions
27 lines (24 loc) · 720 Bytes
/
Caesar.java
File metadata and controls
27 lines (24 loc) · 720 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
//A Java Program to illustrate Caesar Cipher Technique
class Caesar{
// Encrypts text using a shift od s
public static StringBuffer encrypt(String text, int s) {
StringBuffer result= new StringBuffer();
char ch;
for (int i=0; i<text.length(); i++) {
if (Character.isUpperCase(text.charAt(i)))
ch = (char)(((int)text.charAt(i) + s - 65) % 26 + 65);
else
ch = (char)(((int)text.charAt(i) + s - 97) % 26 + 97);
result.append(ch);
}
return result;
}
// Driver code
public static void main(String[] args) {
String text = "Hello KASUN";
int s = 5;
System.out.println("Text : " + text);
System.out.println("Shift : " + s);
System.out.println("Cipher: " + encrypt(text, s));
}
}