Two words or phrases in English are anagrams if their letters (and only their le
ID: 3624642 • Letter: T
Question
Two words or phrases in English are anagrams if their letters (and only their letters), rearranged, are the same. We assume that upper and lower case are indistinguishable, and punctuation and spaces don't count. Some examples and non-examples:* The eyes / they see (yes)
* moo / mo (no)
* Clint Eastwood / Old west Action! (yes)
* Dormitory / Dirty Room (yes)
Your job for this assignment is a slight variant of the traditional anagram problem called sub anagram. Here your job is to write a two class application that reads in two words or phrases from the keyboard, and then judges if the first phrase is an anagram of some of the letters in the second phrase. Here are some examples:
* mo / moo (yes)
* moo / mo (no)
* rip / zipper (yes)
* Clint Eastwood / Old west Action! (yes)
You must use the Scanner class to read in the input strings. Use the nextLine() Scanner method, rather than next(), since spaces may be present in your submissions.
The classes must be called SubAnTester, and SubAnagram
Some tips:
* use the String methods toLowerCase() and toCharArray(). The former takes all letters in a String and changes them to lower case; the latter converts a String into an array of characters. Also, this assignment is - of course - about characters and character matching. For some useful background on characters and how to work with them, watch the movies in the textbook at the ends of section 4.1 and 7.1.
* Very important: suppose you have two arrays of characters such that the first is purported to be an embedded anagram of the second, in the sense we've described above. How can you tell? The single most important thing to do, before you write a single line of code, is to work out a paper and pencil algorithm that satisfactorily distinguishes between anagrams and non-anagrams.
* Your classes must be commented! In particular, each method must have a one line comment just below the header line, which tells what the method is supposed to do.
* Algorithm Idea #1: make a scoreboard for the letters a to z. Every time you encounter a letter in the second String, up its count by 1; Then, every time you encounter a letter in the first String, lower its count by 1. Accept if the scoreboard ends up with all entries >= 0.
* Algorithm Idea #2: convert strings to arrays. March down first array (representing the first string). When you encounter a letter, look for it in the second array. If you find it, blank it out; if you don't find it - not an embedded anagram. When you're all done, you've got an anagram if your search in the second array never goes bad.
Explanation / Answer
I'll give it to you easy:
import java.util.Scanner;
class subAnagram
{
String big, small;
subAnagram()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter large word");
big=s.nextLine().toLowerCase();
System.out.println("Enter small word");
small=s.nextLine().toLowerCase();
check();
}
void check()
{
int index=0;
int count;
for(count=0;count
{
index=big.indexOf(small.charAt(count));
if(index!=-1)
big=big.substring(0,index)+big.substring(index+1);
}
if(count==small.length())
System.out.println("YES it is a sub anagram");
else
System.out.println("Nope");
}
}
If you can't follow it, PM me and I'll explain the algorithm, but it's pretty straightforward and I've tested it using BlueJ !
The only thing is you have to model your classes properly, but the check method holds perfectly. I've initialized the class variables from the constructor itself(I know, its silly, but you get the idea I hope).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.