forked from adityabiswal2147207/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesars.java
More file actions
34 lines (29 loc) · 1.06 KB
/
Copy pathcaesars.java
File metadata and controls
34 lines (29 loc) · 1.06 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
//importing java libraries:-->
import java.io.*;
import java.util.*;
public class caesars {
public static final String alpha = "abcdefghijklmnopqrstuvwxyz";
//making of the encrypt function:-->
public static String encrypt(String message, int shiftkey){
message = message.toLowerCase();
String cipherText = "";
for(int i=0;i<message.length();i++){
int charPosition = alpha.indexOf(message.charAt(i));
int keyVal = (shiftkey + charPosition) % 26;
char varReplace = alpha.charAt(keyVal);
cipherText += varReplace;
}
return cipherText;
}
//making the main function:-->
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
String message = new String();
int key = 0;
System.out.print("Enter the string for encryption:- ");
message= sc.next();
System.out.println("\n\nEnter Shift Key: ");
key = sc.nextInt();
System.out.println("\nEncrypted message: "+ encrypt(message,key));
}
}