Skip to content

Commit a577ce0

Browse files
authored
Caesar Cipher.java
1 parent 01f0fcf commit a577ce0

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Caesar Cipher.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class CaesarCipher
2+
{
3+
public static StringBuffer encrypt(String text, int s)
4+
{
5+
StringBuffer result= new StringBuffer();
6+
7+
for (int i=0; i<text.length(); i++)
8+
{
9+
if (Character.isUpperCase(text.charAt(i)))
10+
{
11+
char ch = (char)(((int)text.charAt(i) +
12+
s - 65) % 26 + 65);
13+
result.append(ch);
14+
}
15+
else
16+
{
17+
char ch = (char)(((int)text.charAt(i) +
18+
s - 97) % 26 + 97);
19+
result.append(ch);
20+
}
21+
}
22+
return result;
23+
}
24+
25+
26+
27+
public static void main(String[] args)
28+
{
29+
String text = "ATTACKATONCE";
30+
int s = 4;
31+
System.out.println("Text : " + text);
32+
System.out.println("Shift : " + s);
33+
System.out.println("Cipher: " + encrypt(text, s));
34+
}
35+
}

0 commit comments

Comments
 (0)