How do I shift array characters to the right in Java?

Viewed 1179

This is what I have:

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

So far I've converted the string to an array, and I've worked out how to shift, but now I'm stuck.

What I want is for the code to start at ch[0], read the character, shift it one to the right (h to i) and then do the same for each character in the array until it reaches the end.

Right now, my code outputs opqrs. I want it to output ifmmp. If I replace the int i = 0 in the for loop with int i = ch[0], it does start at i, but then it just inputs ijklmno...

I want it to read h, output as i, read e, output as f, and so on until it reaches the end of the array.

2 Answers
Related