-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq10.java
More file actions
40 lines (39 loc) · 993 Bytes
/
q10.java
File metadata and controls
40 lines (39 loc) · 993 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
34
35
36
37
38
39
40
/*Implement Caesar Cipher */
// Id - 21CE002 Andrew
import java.util.*;
public class q10
{
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)
{
StringBuffer text= new StringBuffer();
Scanner sc= new Scanner(System.in);
System.out.println("Enter text to Encrypt :");
text.append(sc.next());
System.out.println("Enter shift :");
int s = sc.nextInt();
String str = text.toString();
System.out.println("Text : " + text);
System.out.println("Shift : " + s);
System.out.println("Cipher: " + encrypt(str, s));
}
}