How to convert a letter to an another letter in Java [duplicate]

You could use something like the following algorithm to accomplish this:

// Our input string.
String input = "I love fish";

// Contains the "encrypted" output string.
StringBuilder encrypted = new StringBuilder();

// Process each character in the input string.
for (char c : input.toCharArray()) {
    if (Character.toLowerCase(c) != 'a' && Character.isLetter(c)) {
        // If the character is a letter that's not 'a', convert it to the previous letter.
        char previous = (char) ((int) c - 1);
    } else {
        // Otherwise just append the original character.

// Prints the output to stdout.

Leave a Comment