Question

Two words or phrases in English are anagrams if their letters (and only their letters), rearranged,...

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. Two phrases are anagrams if they contain exactly the same number of exactly the same letters, e.g., 3 A's, 0 B's, 2 C's, and so forth.

Some examples and non-examples of regular anagrams:

* The eyes / they see (yes)
* moo / mo (no)
* Clint Eastwood / Old west Action! (yes)
* Dormitory / Dirty Room (yes)
* Debit card / bad credit (yes)
* One good turn deserves another / Do rogues endorse that? No! Never! (yes)

When are two phrases anagrams? Your job for this assignment is to solve a slight variant of the traditional anagram problem, called the wild anagram problem.

Suppose you have two phrases, which we'll call left, and right. Unlike traditional anagram phrase pairs - the eyes / they see - with a wild anagram pair, the second string, the one on the right, can contain the wild card symbol *, which can stand for any letter. Thus

the eyes / th*y *ee

is a wild pair, with one * standing for e and the other * standing for s

Here are some more examples, with explanations

abc / *** (true: one * for each letter)
The eyes / they see (true - don't have to be *s)
ab / *** (false - *'s MUST stand for something, so right string is too big.
ab / * (false)
xxy / *x* (true - one * for x, one for y)
xxy / y** (true - both *'s for x)
Clint Eastw**d / Old west Action! (false - The symbol *, when it's on the left, is simply ignored - it's just like a comma or period. So there are too many letters on the right.)


Example of a program run:

Enter a phrase:
> Clint Eastwood 
Enter another phrase:
>Old w*st Acti*n! 
true




PROJECT REQUIREMENTS:

  1. Your classes must be called WildTester, and WildGram.

  2. Your WildTester class must prompt the user to enter each phrase separately. It must use the same Scanner object to read in each of the two input phrases.

  3. Your program must print true if the wild anagram relationship is satisfied, and false if it isn't. All console input and output must be done by the WildTester class.

  4. Your WildGram class must have an isWildGram method, which takes two String parameters - the left and right phrases to be tested, and returns a boolean value. The boolean value returned should be true if the wild anagram relationship is satisfied, and false, if it isn't.

  5. Your WildTester class must create an instance of the WildGram class and call the WildGram method isWildGram to determine if the input satisfies the wild anagram relationship.

  6. You MUST use one or more array(s) in an essential way in your solution to this project.

  7. You may NOT import and use the ArrayList class for this project. (An array is not the same thing as an ArrayList.)
  8. Important: for this project upper and lower case are indistinguishable, so after reading the strings, convert them to lower case. Also, remember that except for the * symbol, all punctuation, other symbols, and spaces are ignored.



Some tips:


  1. Remember to use the nextLine Scanner method, rather than next, since spaces may be present in the two phrases that are submitted.
  2. Use the String methods toLowerCase and, possibly, toCharArray, which converts a string into an array of chars. (Consult the Java API).
  3. For some useful background on characters and how to work with them, watch the movies in the textbook at the ends of sections 4.1 and 7.1.
  4. Very important: Work out what's going on with pencil and paper first.



Some algorithms you might try on this problem



Algorithm Idea #1: convert strings to arrays of chars using the string method toCharArray. March down the first array (representing the left string). When you encounter a letter, look for it in the second array. If you find it, blank out the occurrence in the second array; if you don't find it, look in the second array for a * to match with the left-side letter. If you find one, blank that * out; if you don't find one, then the strings aren't a wild anagram pair. When you're all done, you've got a wild anagram if your searches in the right array never goes bad, and if, at the end, there are no letters and no *'s in the second array. Make sense? Do the algorithm with pencil and paper on this pair:

abac / *a*b.


Algorithm Idea #2: make a scoreboard array for the letters a to z. Move across the left string, and every time you encounter a letter, up its count by 1. When you're done you have a complete inventory, of all the letters in the left string: 3 a's, 0 b's, 2 c's, and so forth. Then, move across the right string. Every time you encounter a letter, lower its count on the scoreboard by one. At the same time, count the number of *'s you discover as you traverse the string.

You've found a true wild anagram after traversing left and right strings if 1) the scoreboard ends up with all entries >= 0; and 2) if you add up the final scoreboard entries, which is the number of left string characters that don't have mates in the right string, then that number should equal the count of the *'s in the right string. Once again do the algorithm with pencil and paper for the pair above (notice that for your paper version of the example above you only need three cells in your array, and not twenty-six, since the strings only involve a, b and c.)

IMPORTANT NOTE: Do NOT put import statements in your classes.

import java.util.Scanner;

Place your WildGram class here.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class WildGram
{
   //implemention of 1st Algorithm
   public boolean isWildGram(String left, String right)
   {
       //System.out.println("\nLeft: "+left + "\tRight: "+right);
       //get left string length and right one length
       int lenLeft = left.length();
       int lenRight = right.length();
       //convert both strings to char Array
       char []lefts = left.toLowerCase().toCharArray();
       char []rights = right.toLowerCase().toCharArray();
       //variable to store letter in left array
       char find;
       //index of above char in right array
       int ind;
       //for each char in left
       for(int i = 0;i<lenLeft;i++)
       {
           //if it is letter
           if(Character.isLetter(lefts[i]))
           {
               //copy left i_th letter to find
               find = lefts[i];
               //find the char in right one
               ind = findChar(rights,find);
               //if found
               if(ind != -1)
               {
                   //set it's index to 0 , means block out
                   rights[ind] = 0;
               }
               //if not found
               else
               {
                   //find * and block out that index
                   //if not return false.it's not a wildgram
                   ind = findChar(rights,'*');
                   if(ind == -1)
                   {
                      
                       return false;
                   }
                   else
                   {
                       rights[ind] = 0;
                   }
               }
           }
       }
       //if above steps are done.
       //check for any extra letters remained in right array
       for(int i = 0;i<lenRight;i++)
       {
           //if it is a letter or * wild card return false.
           if(Character.isLetter(rights[i]) || rights[i] == '*')
           {
               return false;
           }
       }
       //finaly nowhere return false executed.
       //then return true
       return true;
   }
   //function that returns given char in a array of chars
   //if not returns -1
   public int findChar(char[] chars,char find)
   {
       for(int i = 0;i<chars.length;i++)
       {
           if(chars[i] != 0 && chars[i] == find)
           {
               return i;
           }
       }
       return -1;
   }
}
import java.util.Scanner;
class WildTester
{
   public static void main(String args[])
   {
      
       /*
       System.out.println("Result: "+test.isWildGram("the eyes","th*y *ee"));
       System.out.println("Result: "+test.isWildGram("abc","***"));
       System.out.println("Result: "+test.isWildGram("The eyes","they see"));
       System.out.println("Result: "+test.isWildGram("ab","***"));
       System.out.println("Result: "+test.isWildGram("xxy","*x*"));
       System.out.println("Result: "+test.isWildGram("xxy","y**"));
       System.out.println("Result: "+test.isWildGram("ab","*"));
       System.out.println("Result: "+test.isWildGram("Clint Eastw**d","Old west Action!"));
       */
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a Phrase: ");
       String left = in.nextLine();
       System.out.println("Enter another Phrase: ");
       String right = in.nextLine();
       //create object of WIldGram and call isWildGram() function
       WildGram test = new WildGram();
       System.out.println(test.isWildGram(left,right));
      
   }
}
C\Windows\SYSTEM32\cmd.exe Enter a Phrase Clint Eastvood Enter another Phrase: 0ld wst Actixn! true Kprogram exited with code

Add a comment
Know the answer?
Add Answer to:
Two words or phrases in English are anagrams if their letters (and only their letters), rearranged,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python Two words are anagrams of each other if they contain the same letters in them....

    python Two words are anagrams of each other if they contain the same letters in them. So left is an anagram of felt and vice-versa. A Super Anagram is a special kind of anagram. A Super Anagram is an anagram whose first and last letters are the same. Your program needs to read in two words on a single line. If the pair of words are Super Anagrams of each other, print out Super Anagram!. If the pair of words...

  • Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able...

    I need a program in fortran 95 that can detect if two strings are anagrams Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able to determine if two inputted texts are anagrams of one another Relevant Details and Formulas: An anagram of a text is a rearrangement of the letters such that it forms another, usually intelligible, set of words. Capitalization is not important and any white space, punctuation, or other non-letter...

  • Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be...

    Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able to determine if two inputted texts are anagrams of one another. Relevant Details and Formulas: An anagram of a text is a rearrangement of the letters such that it forms another, usually intelligible, set of words. Capitalization is not important and any white space, punctuation, or other non-letter symbols should be ignored. Program Specification: * Prompt the user for a pair of text...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • Consider the two strings "doggie dish" and "Drip coffee". What letters do they have in common?...

    Consider the two strings "doggie dish" and "Drip coffee". What letters do they have in common? That is, what is the letter intersection count for the pair of strings? Let's look at 'd' first. The first string has 2 d's (d's occurrence count is 2), while the second has just 1 d (capitalization doesn't count) - so the intersection for the strings for d is 1. What about g? There the occurrence counts are 2 and 0, so the intersection...

  • Write the is_anagram function with details in pic. SENG 265 A01/AD2 Page 14 Qeestion 24 (45...

    Write the is_anagram function with details in pic. SENG 265 A01/AD2 Page 14 Qeestion 24 (45 marks) Write a C function to detennine lÉ one string is an anagras of snothen, Yowr function is emagsm 0 will ake two strings as parsmetens, and will eturens 1 if the string wre anagrams of each other, otherwise it will retuns An anagram is a pbrase that uses all the letters-and exactly those letters-of some other phrase Below are some anagram examples: Zastre:...

  • Background An anagram is a word phrase, or name formed by rearranging the letters of another,...

    Background An anagram is a word phrase, or name formed by rearranging the letters of another, such as the word "cinema", formed from 'iceman". Problem Statement Given two strings s and t, write a function to determine ift is an anagram of S. A sample main function is provided so that you may test your code on sample inputs. Inputs can be provided in the box below your code, please format your input as follows: Example input nose soen Requirements...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make...

    Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics. This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the...

  • Original question IN JAVA Show the user a number of blank spaces equal to the number...

    Original question IN JAVA Show the user a number of blank spaces equal to the number of letters in the word For cat, you would show _ _ _. Prompt a user to guess a letter until they have run out of guesses or guessed the full word By default, the user should be able to make 7 failed guesses before they lose the game. If the user guesses a letter that is in the target word, it does not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT