-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencrpt-cipher.java
More file actions
37 lines (36 loc) · 1.04 KB
/
encrpt-cipher.java
File metadata and controls
37 lines (36 loc) · 1.04 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
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String plaintext = sc.nextLine();
int key = sc.nextInt();
String ciphertext = "";
char cc;
if(key>26){
key = key%26;
}
for(int i=0; i < plaintext.length();i++)
{
cc = plaintext.charAt(i);
if(cc >= 'a' && cc <= 'z')
{
cc = (char) (cc + key);
if(cc > 'z') {
cc = (char) (cc+'a'-'z'-1); //typecasted to character
}
ciphertext = ciphertext + cc;
}
else if(cc >= 'A' && cc <= 'Z') {
cc = (char) (cc + key);
if(cc > 'Z') {
cc = (char) (cc+'A'-'Z'-1);
}
ciphertext = ciphertext + cc;
}
else {
ciphertext = ciphertext + cc;
}
}
System.out.println(" cipher text : " + ciphertext);
}
}