Question: Anagram in Java: An anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce
Anagram in Java:
An anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to
produce a new word or phrase, using all the original letters exactly once1; for example, you can rearrange
the letters of the word cinema to produce iceman.
Your task is to write a simple anagram detector: it compares two phrases and reports whether or not they
use the same number of the same letters (ignoring case and any non-letter symbols). The detector will
work by checking if both phrases use the same number of each letter:
1. The countLetters method counts how many times each letter occurs in a word and returns an
array of integers representing this tally. The 0th element represents how many a or As were found
in the word, the 1st element represents how many b/Bs, and the 25th element how many z/Zs.
All other symbols are ignored in the resulting array.
2. The sameCounts method compares two integer arrays and returns true only if they have the same
contents, including their sizes and values fsor each element. Note: you are not allowed to use
Arrays.equals, or equivalent, to implement this method.
Your main method should prompt the user for two phrases, then report whether or not they are
anagrams; for example
Enter phrase 1: cinema
Enter phrase 2: iceman
These phrases are anagrams.
Enter phrase 1: cinema
Enter phrase 2: snowman
These phrases are not anagrams.
Enter phrase 1: Anagrams!
Enter phrase 2: __Ars Magna__
These phrases are anagrams.
//Given code
int[] counts = new int[26];
s1 = "Enter phrase 1: ";
s2 = "Enter phrase 2: ";
s3 = "These phrases are anagrams.";
s4 = "These phrases are not anagrams.";
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
