-4

I have to Implement a static public method named "encodeCaesar" in the class "Functionality.java", which encodes a text using Caesar encryption and I am a complete novice in Java

Signature: encodeCaesar(String s, int val) : String.

The method gets a string value and an integer value as input parameters. The letters (characters) from the string value are to be shifted by the integer value. For simplicity, I can assume that there are only letters and no spaces, numbers or special characters.

The string value should be converted to lower case before the encryption is performed. The method should return a string where each letter has been moved according to the specified integer value.

Example: encodeCaesar("Ac",3) returns "df". If the given integer value is less than 0 or greater than 26, an empty string should be returned.

public class Functionality {

public static void main(String[] args) {

}

    

public static String caesar(String s, int val) {

            char[] newString = s.toCharArray();
            for(int i = 0; i < s.length(); i++){
                int newChar = newString[i]+val;
                while(newChar > 65+26) // 65 = A, 26 = number of letters in the alphabet
                    newChar -= 26;

                newString[i] = (char) (newChar);
Bala
  • 9
  • 4
  • Arzo there was a problem with the code. I couldn´t post the code although I used Ctrl and K – Bala Nov 17 '20 at 18:39
  • 1
    [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/334823#334823) – Marsroverr Nov 17 '20 at 21:01

1 Answers1

0

You may differenciate the lowercase and uppercase letters, because the bound is not the same. In my code I've used 2 loops, one for each case, that decrease the value if it's upper it's own bound. For the case you're under your bound (using a negative val), you just need the +26 when creating newChar

public static String caesar(String s, int val) {
    char[] newString = s.toCharArray();
    for (int i = 0; i < s.length(); i++) {
        int newChar = newString[i] + val + 26;

        // Handle uppercase letters
        while (Character.isUpperCase(newString[i]) && newChar >= 65 + 26) {
            newChar -= 26;
        }

        // Handle lowecase letters
        while (Character.isLowerCase(newString[i]) && newChar >= 97 + 26) {
            newChar -= 26;
        }

        newString[i] = (char) (newChar);
    }

    return String.valueOf(newString);
}

Tests

System.out.println(caesar("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3).equals("DEFGHIJKLMNOPQRSTUVWXYZABC"));
System.out.println(caesar("ABCDEFGHIJKLMNOPQRSTUVWXYZ", -3).equals("XYZABCDEFGHIJKLMNOPQRSTUVW"));

To treat the input as lowercase remove the uppercase part and add .toLowerCase()

public static String caesar(String s, int val) {
    char[] newString = s.toLowerCase().toCharArray();
    for (int i = 0; i < s.length(); i++) {
        int newChar = newString[i] + val + 26;
        // Handle lowecase letters
        while (Character.isLowerCase(newString[i]) && newChar >= 97 + 26) {
            newChar -= 26;
        }
        newString[i] = (char) (newChar);
    }
    return String.valueOf(newString);
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • First of all Thank you Azro!!!! There is a small mistake in your code. When I enter for example Caesar("Ac",3) it returns "Df" although in my assignment it states it should return "df" – Bala Nov 17 '20 at 22:39
  • @Bala I've edit to add the shorten part for getting only lowercase – azro Nov 17 '20 at 22:43