Java - Replacing a multiple Characters without overwriting the last

ぃ、小莉子 提交于 2019-12-14 03:34:40

问题


I am trying to make some sort of encrypt and decrypt program where it takes a letter and turns it into the next letter on the keyboard (like the following)

 data = data.replace('q', 'w');
 data = data.replace('w', 'e');

(data is a string)

With this code it turns the 'q' into 'w', but then that same 'w' into an 'e' and I don't want that happening. How would I avoid this?


回答1:


This will do the trick:

String data = "...";

StringBuilder finalData = new StringBuilder(data.length());
for(int i = 0; i < data.length() - 1; i++) {
    char replacement = getReplacement(data.charAt(i));
    finalData.append(replacement);
}
finalData.append(data.charAt(data.length() - 1));

String result = finalData.toString();



回答2:


This encoding method is known as Caesar cipher. Simple googling of Caesar cipher would get you lot of code snippets. For your convenience i have attached a code snippet below .

 class CaesarCipher {
    private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
    public String encrypt(String plainText,int shiftKey)
    {
       plainText = plainText.toLowerCase();
       String cipherText="";
       for(int i=0;i<plainText.length();i++)
       {
            int charPosition = ALPHABET.indexOf(plainText.charAt(i));
            int keyVal = (shiftKey+charPosition)%26;
            char replaceVal = this.ALPHABET.charAt(keyVal);
            cipherText += replaceVal;
       }
       return cipherText;
 }
 public String decrypt(String cipherText, int shiftKey)
 {
       cipherText = cipherText.toLowerCase();
       String plainText="";
       for(int i=0;i<cipherText.length();i++)
       {
            int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i));
            int keyVal = (charPosition-shiftKey)%26;
            if(keyVal<0)
            {
                  keyVal = this.ALPHABET.length() + keyVal;
            }
            char replaceVal = this.ALPHABET.charAt(keyVal);
            plainText += replaceVal;
       }
       return plainText;
   }

}

 class CaesarDemo {
 public static void main(String args[])
 {
       String plainText = "studentitzone";
       int shiftKey=4;

       CaesarCipher cc = new CaesarCipher();

       String cipherText = cc.encrypt(plainText,shiftKey);
       System.out.println("Your Plain  Text :" + plainText);
       System.out.println("Your Cipher Text :" + cipherText);

       String cPlainText = cc.decrypt(cipherText,shiftKey);
       System.out.println("Your Plain Text  :" + cPlainText);
 }
}

Where the value for shiftkey determines the number of character you need to shift. for example if shiftkey = 4 then all A will be replaced by D .

Source : http://en.wikipedia.org/wiki/Caesar_cipher
http://beta.studentitzone.com/UI/viewarticle/Caesar-cipher-Encryption-and-Decryption-Program-in-Java

Hope this helps




回答3:


Looks like you are trying to map a key on the keyboard with the key on the right side to it. You can manually map every key to a specific character using HashMap. Adding mappings for so many characters is very tedious! I don't know how to map them dynamically.

public static void foo(String str) {
    HashMap<Character, Character> map = new HashMap<Character, Character>();
    char c;
    StringBuilder sb = new StringBuilder();

    map.put('q', 'w');
    map.put('w', 'e');
    map.put('e', 'r');
    ... // Add some more mappings here
    ...

    for (int i = 0; i < str.length(); i++) {
    c = str.toLowerCase().charAt(i);
    sb.append(map.get(c));
    }
    String result = sb.toString();
    System.out.println(result);
}


来源:https://stackoverflow.com/questions/17902890/java-replacing-a-multiple-characters-without-overwriting-the-last

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!