-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
44 lines (44 loc) · 1.1 KB
/
CaesarCipher.java
File metadata and controls
44 lines (44 loc) · 1.1 KB
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
36
37
38
39
40
41
42
43
44
/*
* Code by @hexaorzo
*/
import java.util.*;
public class CaesarCipher
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter text to cipher:");
String str = sc.nextLine();
System.out.println("Enter the key");
int key = sc.nextInt();
String encrypted = "";
for(int i=0;i<str.length();i++)
{
char ch = str.charAt(i);
if(ch>='a' && ch<='z')
{
ch = (char)(ch+key);
if(ch>'z')
{
ch = (char)(ch-'z'+'a'-1);
}
encrypted += ch;
}
else if(ch>='A' && ch<='Z')
{
ch = (char)(ch+key);
if(ch>'Z')
{
ch = (char)(ch-'Z'+'A'-1);
}
encrypted += ch;
}
else
{
encrypted += ch;
}
}
System.out.println("Encrypted string :");
System.out.println(encrypted);
}
}