-1

I need to cycle through each character in a string, and based on the character that it is, replace it with another character from a char array.

Basically it looks like this:

for (int k = 0; k < messageToBeEncrypted.length(); k++)
    {
        switch(messageToBeEncrypted.charAt(k))
        {
            case 'a' : messageToBeEncrypted.replace('a', cryptList[0]);
            break;
            case 'b' : messageToBeEncrypted.replace('b', cryptList[1]);
            break;
            //it keeps going for each letter of the alphabet

        }
        System.out.println(messageToBeEncrypted);

    }

The char array cryptList is a randomly generated alphabet, "fgtaixnebqwjkzumlydrovsphc" A is to be replaced by f, b by g, and so on. The problem I'm having is that this code prints the exact same message that was inputted, so if the messageToBeEncrypted was ab, instead of fg, it prints ab. How can I fix this? And if theres a clearer more concise way to accomplish this, do tell me. I realize 26 case statements probably isn't the best way to achieve my goal.

Quikdart
  • 53
  • 2
  • 7
  • There was almost this exact question less than an hour ago. `messageTobeEncrypted = messageToBeEncrypted.replace(...);`. – AntonH Mar 18 '14 at 23:05
  • Fixed it. Thanks AntonH! I couldn't upvote your answer since it was a comment. – Quikdart Mar 18 '14 at 23:10

2 Answers2

0

This seems about what your after (minus the brutal switch statement):

public static String substitutionCipher(String str) {
    //i'm assuming that this random alphabet is exactly 26 long, each unique
    char[] crypt = "fgtaixnebqwjkzumlydrovsphc".toCharArray();
    char[] chars = str.toCharArray();
    for(int i = 0; i < chars.length; i++){
        chars[i] = crypt[((int) chars[i]) - 97];
    }
    return new String(chars);
}

But to answer your original question, the reason it's not working is that Strings are immutable. By calling String.replace, it's not modifying your string; that's actually returning the modified version of the string and then just disappearing since you're not storing the return value. You'd need to say:

for (int k = 0; k < messageToBeEncrypted.length(); k++)
{
    switch(messageToBeEncrypted.charAt(k))
    {
        case 'a' : 
            messageToBeEncrypted = messageToBeEncrypted.replace('a', cryptList[0]);
            break;
        case 'b' : 
            messageToBeEncrypted = messageToBeEncrypted.replace('b', cryptList[1]);
            break;
        //etc.

    }
    System.out.println(messageToBeEncrypted);

}

but the way I mentioned previously is a little gentler on memory.

captainroxors
  • 718
  • 7
  • 18
0

If you want to google it the keyword is Substitution cipher.

Here a short help:

String messageToBeEncrypted = "HelloWorld".toLowerCase();
String alphabet = "fgtaixnebqwjkzumlydrovsphcab";
StringBuilder sb = new StringBuilder();
int pos; 
for (int k = 0; k < messageToBeEncrypted.length(); k++)
{
    pos = (messageToBeEncrypted.charAt(k) - 97);
    sb.append(alphabet.charAt(pos));
}

System.out.println(sb.toString());

The number 97 is the offset in the ASCII table... My example is just for small letters, but its not that hard to complete it for every letter.

Ra Mon
  • 127
  • 3