Reputation: 61
I have this java exercise :
1. The Captain Crunch decoder ring works by taking each letter in a string and adding 13 to it. For example, 'a' becomes 'n' and 'b' becomes 'o'. The letters \wrap around" at the end, so 'z' becomes 'm'. Write a method that takes a String and that returns a new String containing the encoded version. You should assume that the String contains upper and lower case letters, and spaces, but no other punc- tuation. Lower case letters should be tranformed into other lower case letters; upper into upper. You should not encode the spaces. 2. Generalize the Captain Crunch method so that instead of adding 13 to the letters, it adds any given amount. Now you should be able to encode things by adding 13 and decode them by adding -13. Try it.
for the second part i tried to generalize the method so that it works with any positive number .. if i encode a String using agiven number then decoding will be by adding (26 - number) ... my problem is for the part where it asks for adding -13 to decode which doesnt work for me ::
public static String captainCrunch(String s, int cod){
int i = 0;
char hat = ' ';
String r = "";
while(i < s.length()){
if(s.charAt(i) >= 'a' && s.charAt(i) <= 'm'){
hat = (char)(s.charAt(i) + cod);
}else if(s.charAt(i) >= 'n' && s.charAt(i) <= 'z'){
hat = (char)(s.charAt(i) - cod);
}else if(s.charAt(i) >= 'A' && s.charAt(i) <= 'M'){
hat = (char)(s.charAt(i) + cod);
}else if(s.charAt(i) >= 'N' && s.charAt(i) <= 'Z'){
hat = (char)(s.charAt(i) - cod);
}else if(s.charAt(i) == ' '){
hat = ' ';
}
r = r + hat;
i++;
}
return r;
}
public static String Decoder(String s ,int cod){
int i = 0;
char hat = ' ';
String r = "";
if(cod >= 26){
cod = cod%26;
}
while(i < s.length()){
if(s.charAt(i) >= 'a' && s.charAt(i) <= 'm'){
hat = (char)(s.charAt(i) + cod);
if(s.charAt(i) != 'a' && (s.charAt(i) + cod) > 'z'){
hat = (char)('a' + (s.charAt(i) + cod - 'z') - 1);
}
}else if(s.charAt(i) >= 'n' && s.charAt(i) <= 'z'){
hat = (char)(s.charAt(i) + cod);
if((s.charAt(i) + cod) > 'z'){
hat = (char)('a' + (cod - 1) -('z' - s.charAt(i)));
}
}else if(s.charAt(i) >= 'A' && s.charAt(i) <= 'M'){
hat = (char)(s.charAt(i) + cod);
if(s.charAt(i) != 'A' && (s.charAt(i) + cod) > 'Z'){
hat = (char)('A' + (s.charAt(i) + cod - 'Z') - 1);
}
}else if(s.charAt(i) >= 'N' && s.charAt(i) <= 'Z'){
hat = (char)(s.charAt(i) + cod);
if((s.charAt(i) + cod) > 'Z'){
hat = (char)('A' + (cod - 1) -('Z' - s.charAt(i)));
}
}else if(s.charAt(i) == ' '){
hat = ' ';
}
r = r + hat;
i++;
}
return r;
}
public static void main(String[] args){
System.out.println(Decoder("Hello World", 12));
System.out.println(Decoder("Hello World", 14));
System.out.println(captainCrunch("Hello World", 13));
System.out.println(Decoder("Hello World", 13));
System.out.println(captainCrunch("Uryyb Jbeyq", 13));
System.out.println(captainCrunch("Uryyb Jbeyq", -13));
}
Note : the first method works only for cod = 13 as far as i know and second method i made works with any positive number.
any suggestions???
Upvotes: 0
Views: 786
Reputation: 2330
You can do this using only one function encodeDecode
like this:
/* encodeOrDecode = 0 for encode
** encodeOrDecode = 1 for decode
*/
public static String encodeDecode(int encodeOrDecode, String s, int code) {
String result = "";
if (encodeOrDecode == 1) {
code *= -1;
}
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isUpperCase(ch)) {
ch += code;
if (ch > 'Z') {
ch -= 26;
} else if (ch < 'A') {
ch += 26;
}
} else if (Character.isLowerCase(ch)) {
ch += code;
if (ch > 'z') {
ch -= 26;
} else if (ch < 'a') {
ch += 26;
}
}
result += ch;
}
return result;
}
Upvotes: 1
Reputation: 423
I think when you are generalizing, then rather than checking for "s.charAt(i) <= 'm'" this condition, you should check for "s.charAt(i) <= ('a'+cod)" condition. I am not sure but give it a try. Hope this helps. (This is in regards of generalizing first method.)
Upvotes: 0