Anagram Checker
In this lab, you are tasked with implementing a method named isAnagram
in Java. This method will determine whether two given strings are anagrams of each other.
Requirements:
- Method Name:
isAnagram
- Class Name: The method should be in a class named
Main
. - Parameters: The method takes two string parameters,
str1
andstr2
. - Return Type: The method should return a boolean value -
true
ifstr1
andstr2
are anagrams, andfalse
otherwise.
What is an Anagram?
Two strings are anagrams if they contain the same characters in any order, ignoring case, spaces, and punctuation.
Edge Cases and Examples:
-
Identical Strings: Identical strings are always anagrams of each other. For example,
isAnagram("test", "test")
should returntrue
. -
Different Case Letters: The method should be case-insensitive. For instance,
isAnagram("Listen", "Silent")
should returntrue
. -
Non-Anagrams: If the strings do not contain the same characters, the method should return
false
. For example,isAnagram("hello", "world")
returnsfalse
. -
Spaces and Punctuation: Spaces and punctuation should not affect the anagram check. For instance,
isAnagram("Astronomer", "Moon starer")
should also returntrue
.
Instructions:
Implement the isAnagram
method in the Main
class. Ensure it handles the various cases and edge conditions effectively. Your implementation will be tested against several scenarios to validate its correctness.
Challenges
Challenge 1: Identical Strings
- Description: Implement the
isAnagram
method to returntrue
when both input strings are identical. The method should recognize that any string is an anagram of itself.
Challenge 2: True Anagrams
- Description: Ensure that your
isAnagram
method returnstrue
for pairs of strings that are true anagrams of each other. Example: The method should returntrue
for inputs like 'listen' and 'silent'.
Challenge 3: Non-Anagrams
- Description: Your
isAnagram
method should returnfalse
when the input strings are not anagrams. For instance, it should returnfalse
for inputs like 'hello' and 'world'.