Search code examples
javaarraysloopsanagram

How to refacor a code use only loops and simple arrays?


I wrote that code and it's working. But I need to refactor it. I can use only simple methods for solving the problem, for example: "for" loops and simple array.

public class Anagram {

public static void main(String[] args) throws IOException {

    Anagram anagrama = new Anagram();

   try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));) {

        System.out.println("Enter word or phrase: ");
        String userText = reader.readLine();
        String resultAnagrama = anagrama.makeAnagram(userText);
        System.out.println("Result of Anagrama : " + resultAnagrama);                   
   }      
}

This method take user's text and make anagram, but all non-letters should stay on the same places

/**
 * @param text
 * @return reversed text and all non-letter symbols stay on the same places
 */
public String makeAnagram(String text) {

    HashMap<Integer, Character> mapNonLetters;

    String[] textFragments = text.split(" ");
    StringBuilder stringBuilder = new StringBuilder();

    //Check each elements of array for availability symbols and make reverse of elements
    for (int i = 0; i < textFragments.length; i++) {
        char[] arrayCharacters = textFragments[i].toCharArray();
        mapNonLetters = saerchNonLetters(arrayCharacters); // search symbols

        StringBuilder builderAnagramString = new StringBuilder(textFragments[i]);

        //Delete all non-letters from element of array
        int reindexing = 0;
        for (HashMap.Entry<Integer, Character> entry : mapNonLetters.entrySet()) {
            int key = entry.getKey();
            builderAnagramString.deleteCharAt(key - reindexing);
            reindexing ++;
        }

        builderAnagramString.reverse();

        //Insert all non-letters in the same places where ones stood
        for (HashMap.Entry<Integer, Character> entry : mapNonLetters.entrySet()) {
            int key = entry.getKey();
            char value = entry.getValue();
            builderAnagramString.insert(key, value);
        }

        textFragments[i] = builderAnagramString.toString(); 
        stringBuilder.append(textFragments[i]); 

        if (i != (textFragments.length - 1)) {
            stringBuilder.append(" ");
        }
        mapNonLetters.clear();
    }
    return stringBuilder.toString();
}

This method search all non-letters from each worв of user's text

/**
 * Method search symbols
 * @param arrayCharacters
 * @return HashMap with symbols found from elements of array
 */
public HashMap<Integer, Character> saerchNonLetters(char[] arrayCharacters) {

    HashMap<Integer, Character> mapFoundNonLetters = new HashMap<Integer, Character>();

        for (int j = 0; j < arrayCharacters.length; j++) {
            //Letters lay in scope 65-90 (A-Z) and 97-122 (a-z) therefore other value is non-letter
            if (arrayCharacters[j] < 65 || (arrayCharacters[j] > 90 && arrayCharacters[j] < 97) ||
                    arrayCharacters[j] > 122) {
                mapFoundNonLetters.put(j, arrayCharacters[j]);
            }
        }
        return mapFoundNonLetters;
    }
}

Solution

  • public class Anagram {
    public static void main(String[] args) {
        String text = "!Hello123 ";
        char[] chars = text.toCharArray();
    
        int left = 0;
        int right = text.length() - 1;
    
        while (left < right) {
            boolean isLeftLetter = Character.isLetter(chars[left]);
            boolean isRightLetter = Character.isLetter(chars[right]);
            if (isLeftLetter && isRightLetter) {
                swap(chars, left, right);
                left++;
                right--;
            } else {
                if (!isLeftLetter) {
                    left++;
                }
    
                if (!isRightLetter) {
                    right--;
                }
            }
        }
    
        String anagram = new String(chars);
        System.out.println(anagram);
    }
    
    private static void swap(char[] chars, int index1, int index2) {
        char c = chars[index1];
        chars[index1] = chars[index2];
        chars[index2] = c;
    }
    

    }