Java - queastion on purpose of (char){Alphabet - "a" + "z" + 1}

Viewed 42

Essentially, I am confused on the purpose of this code and what it does ( cAlphabet = (char)(cAlphabet - 'a' + 'z' + 1); ), in this encryption code, could someone please explain how this works thanks!

 System.out.println(" Input the ciphertext message : ");
         
            String ciphertext = sc.nextLine();
             System.out.println(" Enter the shift value : ");
             int shift = sc.nextInt();
             String decryptMessage = "";
             for (int i = 0; i < ciphertext.length(); i++)

             {
                // Shift one character at a time
                char alphabet = ciphertext.charAt(i);
                
                // if alphabet lies between a and z
                if (alphabet >= 'a' && alphabet <= 'z') 
                {
                   // shift alphabet
                   alphabet = (char)(alphabet + shift);

                // shift alphabet less than 'a'
                if (cAlphabet < 'a')
                {
                    //reshift to starting position
                    cAlphabet = (char)(cAlphabet - 'a' +'z' + 1);
                }
1 Answers
cAlphabet = (char)(cAlphabet - 'a' + 'z' + 1);

When you make some operations(sum, minus, etc) with Char it wiil use numeric code of character. So you will get new numeric code and convert it back to the character.

this is almost the same as cAlphabet = (char)(cAlphabet - 219 + 97 + 1);

Related