0

This is what I have right now:

class encoded{
    public static void main(String[] args){
        int shift = In.getInt();
        String s1 = "bool";
        char[] ch=s1.toCharArray();
        for(int i=0;i<ch.length;i++){  
            char c = (char) (((ch[i] - 'a' - shift) % 26) + 'a');
            System.out.print(c);
        }
    }
}

This code works to shift all characters in the string left by however much I want it to shift. The problem arises when, for example, I shift 'abc' 1 left, it returns '`ab'. What I want is for the characters to wrap back around to 'z' instead, so that the shifted 'abc' becomes 'zab' instead. How would I go about doing this? The characters need to always wrap back around to z when they're shifted left of 'a', and need to wrap back around to a when they're shifted right of 'z' (this would be done by changing "'a' - shift" to "'a' + shift". Thanks so much in advance!

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
Ali Nisar
  • 39
  • 4
  • Hint: shift some of your characters to the right in your code. You want us to spend our time to help you, but then you drop such an unreadable mess on us! – GhostCat May 22 '17 at 18:34
  • Right now, I'm mainly focusing on how to turn '`ab' into 'zab' by wrapping around to z. To shift right, I would only have to change (ch[i] - 'a' - shift) into (ch[i] - 'a' + shift). Regardless of which way I shift, my question is about how to wrap around instead of just continuing down the ASCII values. My apologies if it looks confusing. – Ali Nisar May 22 '17 at 18:39
  • your question was answered 12 hours ago... either accept it or give feedback so the people that took the time to helping you can correct their suggestions.... – ΦXocę 웃 Пepeúpa ツ May 22 '17 at 18:42
  • @ΦXocę웃Пepeúpaツ Thanks for that hint. I didnt notice that the dup by Bing was actually to the *same* user. Now thats tricky - as this question does contain an additional aspect (the replacing of a with z). Not sure what to do about it. – GhostCat May 22 '17 at 18:44
  • Actually the original question was about right-shifts, this one is about left-shifts. The solutions in the original question don't work for left-shifts. Looks like a homework problem anyways. – nucleon May 22 '17 at 18:51

2 Answers2

0

Not doing your homework for you, but giving you some hints:

char c = (char) (((ch[i] - 'a' - shift) % 26) + 'a');

You see, you are doing the shift operation unconditionally. Instead, read about using the if statement. You want to do your program different things, compared on the value of ch[i]. For some cases, just "shift", for others to replace values.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

The problem is that the modulo operation can yield negative results. In Java8 you can use Math.floorMod as an alternative:

class Main{
    public static void main(String[] args){
        int shift = 1;
        String s1 = "abool";
        char[] ch=s1.toCharArray();
        for(int i=0;i<ch.length;i++){  
            char c = (char) ((Math.floorMod((ch[i] - 'a' - shift), 26)) + 'a');
            System.out.print(c);
        }
    }
}

An alternative would be to use char c = (char) ( (((ch[i] - 'a' - shift) % 26) + 26) % 26 + 'a');

nucleon
  • 1,128
  • 1
  • 6
  • 19