Is there any method to reverse words with retaining position's case

I need a function which accepts a string as input and reverser words with retaining position's case.

Example:

Input: "HeLlo woRld BEn"

Output: "olLeh dlRow NEb"

Upvotes: 0

Views: 1142

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79580

Do it as follows:

public class Main {
    public static void main(String[] args) {
        System.out.println(reverseWordsRetainingPositionCase("HeLlo woRld BEn"));
    }

    static String reverseWordsRetainingPositionCase(String str) {
        String[] words = str.split("\\s+");// Split the string on space(s)
        StringBuilder sb = new StringBuilder(str);// Create a StringBuilder instance with the content of str
        for (String word : words) {
            // Find the word in sb and replace it with its reverse
            sb.replace(sb.indexOf(word), sb.indexOf(word) + word.length(), reverseCharsRetainingPositionCase(word));
        }
        return sb.toString();
    }

    static String reverseCharsRetainingPositionCase(String word) {
        StringBuilder reversedWord = new StringBuilder(word).reverse();// Reverse the word
        for (int i = 0; i < word.length(); i++) {
            // If the character at i in the word is in upper case, change the case of the
            // character at i in the reversed word to upper case. Process the reversed word
            // in the same way for lower case.
            if (Character.isUpperCase(word.charAt(i))) {
                reversedWord.setCharAt(i, Character.toUpperCase(reversedWord.charAt(i)));
            } else if (Character.isLowerCase(word.charAt(i))) {
                reversedWord.setCharAt(i, Character.toLowerCase(reversedWord.charAt(i)));
            }
        }
        return reversedWord.toString();
    }
}

Output:

OlLeh dlRow NEb

I have put enough comments in the code to make it easier for understanding. Feel free to comment in case of any doubt/issue.

Upvotes: 0

Jason
Jason

Reputation: 5244

We split the original sentence into words and reverse them using StringBuilder@reverse. We then create a character array representing the original words and the reversed words. We then loop through all characters and make sure the case is correct by checking for upper and lower case.

    String reverse(String sentence) {
        char[] characters = sentence.toCharArray();

        String reversed = Stream.of(sentence.split(" "))
                .map(word -> new StringBuilder(word).reverse())
                .collect(Collectors.joining(" "));

        char[] reversedCharacters = reversed.toCharArray();

        for (int index = 0; index < reversedCharacters.length; index++) {
            char characterAtIndex = characters[index];

            char characterAtReversedIndex = reversedCharacters[index];

            if (Character.isUpperCase(characterAtIndex) && Character.isLowerCase(characterAtReversedIndex)) {
                reversedCharacters[index] = Character.toUpperCase(characterAtReversedIndex);
            } else if (Character.isLowerCase(characterAtIndex) && Character.isUpperCase(characterAtReversedIndex)) {
                reversedCharacters[index] = Character.toLowerCase(characterAtReversedIndex);
            }
        }
        return new String(reversedCharacters);
    }

Upvotes: 2

Related Questions