Reverse only letters in string and keep the same order of the words

Alex :

I need to reverse only letters in String and keep symbols and numbers in the same place using Character.isLetter and also I need to keep the same order of the reversed words. My code reverses a string and keeps symbols and numbers in place but changes the order of the words, for example:

My input string:

a1bcd efg!h

My output string:

h1gfe dcb!a

Instead my output supposes to be:

d1cba hgf!e

class AnagramsMaker {
    public static String createAnagram(String StringToReverse) {
        char[] stringToChar = StringToReverse.toCharArray();
        int arrayStart = 0;
        int arrayEnd = stringToChar.length - 1;

        while (arrayStart < arrayEnd) {
            if (Character.isLetter(stringToChar[arrayStart]) && Character.isLetter(stringToChar[arrayEnd])) {
                char temp = stringToChar[arrayStart];
                stringToChar[arrayStart] = stringToChar[arrayEnd];
                stringToChar[arrayEnd] = temp;

                arrayStart++;
                arrayEnd--;
            }
            else if (Character.isLetter(stringToChar[arrayStart]) && !Character.isLetter(stringToChar[arrayEnd])) {
                arrayEnd--;
            }
            else if (!Character.isLetter(stringToChar[arrayStart]) && Character.isLetter(stringToChar[arrayEnd])) {
                arrayStart++;
            }
            else {
                arrayStart++;
                arrayEnd--;
            }
        }

        return String.valueOf(stringToChar);
    }
}
Nexevis :

With the way you currently you have it, you don't actually need to modify your method at all and can instead utilize it to reverse each word instead of the entire String at once.

I renamed your current method to public static String createAnagramWord(String StringToReverse) and then created the method below:

public static String createAnagram(String str) {
    String anagram = "";
    String [] arr = str.split(" ");

    for (String s : arr) {
        anagram += createAnagramWord(s) + " ";
    }
    return anagram;
}

Where I use your current method with createAnagramWord(s) inside of it.

In this method note that I split the String passed to it on blank spaces with " " (you can also use \\s+ instead, see this post), and then iterate on the array returned from split using an enhanced for loop.

Inside of the for loop the new String is created by appending each reversed word along with a space.

Example Run:

public static void main(String [] args) { 
    String reverse = "a1bcd efg!h";
    System.out.println(createAnagram(reverse));
}

Output:

d1cba hgf!e

Note:
Appending String in a loop should technically utilize StringBuilder, however, I believed that is out of scope of this question and decided to omit it.

Additionally, you can rework this logic into your current method if for some reason you prefer not to separate the logic into two separate methods.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=421851&siteId=1