-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcypherSolution.java
51 lines (38 loc) · 1.59 KB
/
cypherSolution.java
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
package com.muthu;
import java.io.IOException;
import javax.swing.text.AbstractDocument.LeafElement;
public class cypherSolution {// Complete the caesarCipher function below.
static String caesarCipher(String s, int k) {
String originalAlphabet = "abcdefghijklmnopqrstuvwxyz";
String alphabetRoatated = leftrotate(originalAlphabet, k);
String originalAlphabetCapital = originalAlphabet.toUpperCase();
String alphabetRoatatedCapital = leftrotate(originalAlphabetCapital, k);
System.out.println(alphabetRoatated);
String result = null;
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
System.out.println(c);
if(Character.isAlphabetic(c)) {
int position = originalAlphabet.indexOf(c);
if(position != -1) {
result = result == null ? String.valueOf(alphabetRoatated.charAt(position)) : result+alphabetRoatated.charAt(position);
} else {
position = originalAlphabetCapital.indexOf(c);
result = result == null ? String.valueOf(alphabetRoatatedCapital.charAt(position)) : result+alphabetRoatatedCapital.charAt(position);
}
} else {
result = result + c;
}
}
return result;
}
private static String leftrotate(String str, int d)
{
String ans = str.substring(d) + str.substring(0, d);
return ans;
}
public static void main(String[] args) throws IOException {
String result = caesarCipher("middle-Outz", 2);
System.out.println(result);
}
}