Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

Can't compare 2 strings if a letter in first string exists in second string - java

However I had an assignment of programming in java related to a text i already have under (text). the function is supposed to as below

getEncryptedText(int shift)

return a string representation of ciphertext given that the text to be manipulated is the plaintext using Caesar Cipher.

The number of rotation is depend on the shift value; positive shift value represent the right rotation while negative shift value represent left rotation. However, unlike explain in Wikipedia, this method used following string as plain: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Other characters than above will be treated as it is (i.e. will not been encrypted)

*Further reading: https://en.wikipedia.org/wiki/Caesar_cipher


So this is the class method I have made so far and wanted to know how can i keep the text chars which aren't included in the plaintext i have such as "!,@,#,$,%... and so on". So far i tried everything but couldn't make it but the rest seems fine!

public String getEncryptedText(int shift) {
  
    String ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    String cipherText = "";
    for (int i = 0; i < text.length(); i++){
    {
        int charPosition = ALPHABET.indexOf(text.charAt(i));
        
        if(text.charAt(i) == ' ') {
            
            cipherText += " ";
        
        }
        
        else
        {      
        int keyVal = (shift + charPosition) % 62;
        
        char replaceVal = ALPHABET.charAt(keyVal);
        cipherText += replaceVal;
        }
    }
    }
    return cipherText;
}
question from:https://stackoverflow.com/questions/65887578/cant-compare-2-strings-if-a-letter-in-first-string-exists-in-second-string-ja

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider modifying your if statement and using the StringBuilder class:

class Main {
    public static void main(String[] args) {
        CesarCypherHelper cesarCypherHelper = new CesarCypherHelper();
        System.out.println(cesarCypherHelper.getEncryptedText("Hello World!", 2));
        System.out.println(cesarCypherHelper.getEncryptedText("Hello World!", 64));
    }
}

class CesarCypherHelper {
    public String getEncryptedText(String text, int shift) {
        String ALPHABET =
                "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        StringBuilder encryptedText = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            int charPosition = ALPHABET.indexOf(ch);
            if (charPosition == -1) {
                encryptedText.append(ch);
            } else {
                int keyVal = (shift + charPosition) % ALPHABET.length();
                encryptedText.append(ALPHABET.charAt(keyVal));
            }
        }
        return encryptedText.toString();
    }
}

Output:

Jgnnq Yqtnf!
Jgnnq Yqtnf!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...