0

so I need to encrypt and decrypt using this codebook.

cijuskla....t, there are 26 of them. so c becomes a and i becomes b and j becomes c...and t becomes z.

I have a same set for the capital letters.

The first thing that pop into my head was using endless else if.

for(int i = 0; i < input.length(); i++) {
    if input.charAt[i] == 'a' {
         input.charAt[i] == 'c' }
    else if input.charAt[i] ==

is there a better way to do it I'm missing??

user2998413
  • 73
  • 1
  • 2
  • 7
  • what about `switch` statement? or just use predefined HashMap with all mappings? – Vadim Sep 13 '18 at 16:10
  • @ i need 50 cases then including capitals.... – user2998413 Sep 13 '18 at 16:11
  • Maps are definitely the way to go, for some inspiration on how to initialize the decoding map, have a look [here](https://stackoverflow.com/questions/507602/how-can-i-initialise-a-static-map) – fvu Sep 13 '18 at 16:26

3 Answers3

3

Use a Map to store the key value pair. Look up the value with the key input.charAt[i] and replace. No need for endless if-else

    String input = "abcde";
    Map<Character, Character> encodedChar = new HashMap<Character, Character>();

    encodedChar.put('a', 'c');
    encodedChar.put('b', 'e');

    char[] tempInput = input.toCharArray();

    for (int i = 0; i < tempInput.length; i++) {
        tempInput[i] = encodedChar.get(tempInput[i]);
    }
    input = new String(tempInput);
Ashraff Ali Wahab
  • 1,088
  • 9
  • 19
  • Im sorry but even with the key pair array dont i need to list all in the for loop? do you mind giving me some examples? – user2998413 Sep 13 '18 at 16:15
  • @user2998413 the map would map original letters (keys of the map) to encoded letters (values of the map), so you'd just have to get the value associated to your input letter to get its encoded version. All the mapping would be described in a 26-entries Map instead of your huge if/then/else. – Aaron Sep 13 '18 at 16:21
  • Alternatively you can use two arrays : one with the standard alphabet, the second with the encoded alphabet (`cijuskla....t`) ; you look up the index of the input letter in the standard alphabet, then output the character at the same position in the encoded alphabet. The Map version only enables you to do so in a more streamlined way. – Aaron Sep 13 '18 at 16:24
  • @Aaron yes i was giving two array a shot but still stuck ,i havent learnt hash yet... – user2998413 Sep 13 '18 at 16:29
  • @user2998413 Example added. – Ashraff Ali Wahab Sep 13 '18 at 17:01
1

I tried and code will be like below. Some comments on code to understand clearly :

//add all letters key value pair to this list
final HashMap<Character, Character> letterMapForDecrypt = new HashMap<>();
letterMapForDecrypt.put('c', 'a');
letterMapForDecrypt.put('d', 'b');
letterMapForDecrypt.put('f', 'k');
letterMapForDecrypt.put('h', 'j');

//adding reverse type of decrpyt letter list
final HashMap<Character, Character> letterMapForEncrypt = new HashMap<>();
letterMapForDecrypt.forEach((key, value) -> letterMapForEncrypt.put(value, key));

//decrpyte
String stringToDecrypt = "cddfh";
final char[] charsOfDecrpyt = stringToDecrypt.toCharArray();
for (int i = 0; i < charsOfDecrpyt.length; i++) {
    //get value map and change this char
    charsOfDecrpyt[i] = letterMapForDecrypt.get(charsOfDecrpyt[i]);
}

System.out.println(charsOfDecrpyt);


//encrpte
String stringToEncrypt = "cddfh";
final char[] charsOfEncrypt = stringToEncrypt.toCharArray();
for (int i = 0; i < charsOfEncrypt.length; i++) {
    //get value map and change this char
    charsOfDecrpyt[i] = letterMapForEncrypt.get(charsOfDecrpyt[i]);
}

System.out.println(charsOfEncrypt);
drowny
  • 2,067
  • 11
  • 19
1

You could use a switch instead:

for(int i = 0; i < input.length(); i++) {
    switch (input.charAt[i]) {
        case 'a': input.charAt[i] = 'c';
                  break;
        case 'i': input.charAt[i] = 'b';
                  break;
        //...
        default: break; // nothing to do for the rest
}