How to check if two strings are anagrams, even if one string has a capital

Viewed 17

I successfully created a method that checks if two input strings are anagrams but I haven't been able to figure out how to return true if they're anagrams even if one letter is capital. I have a sort method that I want to use in the areAnagrams method.

/**
* @param str the string to be sorted.
* @return the lexicographically-sorted version of the input string.
*/
public static String sort(String str) {
        // if the string is empty, return null
        if (str.isEmpty()) {
            return null;
        }

        // convert the string into a character array
        char arr[] = str.toCharArray();

        // sort the array
        Arrays.sort(arr);

        // convert the array back to a string for our return type
        String newString = new String(arr);

        return newString;
    }

/**
     * @param str1 the first input string to compare.
     * @param str2 the second input string to compare.
     * @return true if the two input strings are anagrams of each other, otherwise
     *         returns false.
     */
    public static boolean areAnagrams(String str1, String str2) {
        // create new strings that are sorted versions of our parameters
        String newStr1 = sort(str1);
        String newStr2 = sort(str2);

        // if the first new string equals the second new string, return true
        if (newStr1.equals(newStr2))
            return true;

        return false;
    }
0 Answers
Related