-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
33 lines (32 loc) · 961 Bytes
/
Copy pathCaesarCipher.java
File metadata and controls
33 lines (32 loc) · 961 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
import java.util.Scanner;
public class CaesarCipher
{
//Cipher(n) = Decipher(26-4)
public static StringBuffer encrypt(String text, int s)
{
StringBuffer result = new StringBuffer();
for(int i=0; i<text.length(); i++)
{
if(Character.isUpperCase(text.charAt(i)))
{
char ch = (char)(((int)text.charAt(i) + s - 65)% 26 + 65);
result.append(ch);
}
else
{
char ch = (char)(((int)text.charAt(i) + s-97)%26 +97);
result.append(ch);
}
}
return result;
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter text:");
String text = scan.nextLine();
System.out.print("Enter shift:");
int shift = scan.nextInt();
System.out.println("Cipher:" + encrypt(text,shift));
}
}