Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
1.3 kB
2
Indexable
import java.io.*;
import java.util.*;

class UserMainCode {
    public String returnTheModifiedSentence(String input) {
        String[] words = input.split(" ");
        StringBuilder result = new StringBuilder();

        for (String word : words) {
            int shift = word.length();
            StringBuilder modifiedWord = new StringBuilder();

            for (char c : word.toCharArray()) {
                if (Character.isLetter(c)) {
                    char shiftedChar = (char) (c + shift);

                    // Handling lowercase letters
                    if (Character.isLowerCase(c) && shiftedChar > 'z') {
                        shiftedChar = (char) ('a' + (shiftedChar - 'z' - 1));
                    }
                    // Handling uppercase letters
                    else if (Character.isUpperCase(c) && shiftedChar > 'Z') {
                        shiftedChar = (char) ('A' + (shiftedChar - 'Z' - 1));
                    }

                    modifiedWord.append(shiftedChar);
                } else {
                    modifiedWord.append(c); // Keep non-letter characters unchanged
                }
            }
            result.append(modifiedWord).append(" ");
        }

        return result.toString().trim(); // Remove trailing space
    }
}
Leave a Comment