1

I'm trying to solve Caesar's Cipher in Java but there's a twist to it. The input string has alphanumeric values and I am unable to solve. Here's what I've attempted so far:

  String rotationalCipher(String input, int rotationFactor) {
    // Write your code here
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < input.length(); i++) {
      if(Character.isLowerCase(input.charAt(i))) {
         char ch = (char)(((int)input.charAt(i) + rotationFactor - 97) % 26 + 97); 
      } else if (Character.isUpperCase(input.charAt(i))) {
          char ch = (char)(((int)input.charAt(i) + rotationFactor - 65) % 26 + 65); 
          sb.append(ch); 
      } else {
          char ch = (char)(((int)input.charAt(i) + rotationFactor - 48) % 10 + 48); 
          sb.append(ch); 
      }
    }
      return sb.toString();
  }

What I'm trying to do is evaluate each case using its ASCII values but I don't seem to get the desired output. Am I using ASCII wrong? Thanks for your help!

Sample input/output:
input = Zebra-493?
rotationFactor = 3
output = Cheud-726?
youngdev
  • 535
  • 2
  • 7
  • 15

3 Answers3

2

You have two major problems.

  • You did not update StringBuilder with an append for lowercase transitions.
  • You need to handle digits specially using isDigit just like upper and lower case so that you can then ignore characters like - and ?

A couple of suggestions.

  • just assign ch when you first enter the loop and then use it throughout the loop. No need to keep typing in all the input stuff.
  • only append ch to the StringBuilder once near the end when you exit the if/else blocks.
  • Instead of numbers like 97 and 65 use 'a' and 'A'. Less likely to make mistakes that way.

Once you make those changes, your code works just fine.

WJS
  • 36,363
  • 4
  • 24
  • 39
1

Below code works for me-

  String rotationalCipher(String input, int rotationFactor) {
        // Write your code here
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < input.length(); i++) {
            char x = input.charAt(i);
            if (Character.isLowerCase(x)) {
                char ch = (char) ((x + rotationFactor - 97) % 26 + 97);
                sb.append(ch);
            } else if (Character.isUpperCase(x)) {
                char ch = (char) ((x + rotationFactor - 65) % 26 + 65);
                sb.append(ch);
            } else if (Character.isDigit(x)) {
                char ch = (char) ((x + rotationFactor - 48) % 10 + 48);
                sb.append(ch);
            } else {
                sb.append(x);
            }

        }
        return sb.toString();
    }
deeksha gupta
  • 151
  • 1
  • 6
0
String rotationalCipher(String input, int rotationFactor) {
String output = "";
for(char a: input.toCharArray()){
   if(Character.isAlphabetic(a)){
     char startLetter = Character.isUpperCase(a) ? 'A' : 'a';
     output += (char) ((a- startLetter + rotationFactor) % 26 + startLetter);
   }
  else if(Character.isDigit(a)){
    output += (char) ((a + rotationFactor - 48) % 10 + 48);
   }
  else{
    output += a;
  }
}
return output;}
  • System.out.println(rotationalCipher(rotationalCipher("redzbmipor@gmail.com", 15), -15)) prodce bad results...o am I wrong wit decoding? – Ján Яabčan Jun 23 '21 at 11:08