-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
98 lines (98 loc) · 1.46 KB
/
CaesarCipher.java
File metadata and controls
98 lines (98 loc) · 1.46 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.*;
import java.util.*;
class CaesarCipher
{
public static void main(String[]args)throws IOException
{
BufferedReader venki=new BufferedReader(new InputStreamReader(System.in));
int l=Integer.parseInt(venki.readLine());
String s=venki.readLine();
int k=Integer.parseInt(venki.readLine());
int sum=0,chr=0,flag;
for(int i=0;i<l;i++)
{
flag=0; chr=0; sum=0;
int ch=(int)s.charAt(i);
if(ch>=97 && ch<=122)
{
sum=ch+k;
if(sum>122)
{
if(k>26)
{
chr=ch+26;
while(flag!=1)
{
if(Math.abs(chr-sum)<26)
{
sum=ch+(Math.abs(chr-sum));
if(sum>122)
{
sum-=122;
ch=sum+96;
}
else
{
ch=sum;
}
flag=1;
break;
}
chr=chr+26;
}
}
else
{
sum-=122;
ch=96+sum;
}
}
else
{
ch+=k;
}
}
else if(ch>=65 && ch<=90)
{
sum=ch+k;
if(sum>90)
{
chr=ch+26;
if(k>26)
{
while(flag!=1)
{
if(Math.abs(chr-sum)<26)
{
sum=ch+(Math.abs(chr-sum));
if(sum>90)
{
sum-=90;
ch=sum+64;
}
else
{
ch=sum;
}
flag=1;
break;
}
chr+=26;
}
}
else
{
sum-=90;
ch=64+sum;
}
}
else
{
ch+=k;
}
}
char c=(char)ch;
System.out.print(c);
}
}
}