Question
How would i modify the following code to accept user input using a Scanner (JAVA) - This is an Anagram project (the question is attached
How would i modify the following code to accept user input using a Scanner (JAVA)
- This is an Anagram project (the question is attached at the bottom)
import java.util.*;
public class AnagramProject {
static int NO_OF_CHARS = 256;
static boolean areAnagram(char str1[], char str2[]) {
int count1[] = new int[NO_OF_CHARS];
Arrays.fill(count1, 0);
int count2[] = new int[NO_OF_CHARS];
Arrays.fill(count2, 0);
int i;
for (i = 0; i
i++) {
count1[str1[i]]++;
count2[str2[i]]++;
}
if (str1.length != str2.length)
return false;
for (i = 0; i NO_OF_CHARS; i++)
if (count1[i] != count2[i])
return false;
return true;
}
public static void main(String args[]) {
char str1[] = ("").toCharArray();
char str2[] = ("").toCharArray();
if (areAnagram(str1, str2))
System.out.println("The two strings are" + " anagrams of each other");
else
System.out.println("The two strings are not" + " anagrams of each other");
}
}
Anagrams Project Write a program that tests whether two words are anagrams (use exactly the same letters the same number of times) Enter first word: smartest. Enter second word: mattress The words are anagrams. Enter first word: dumbest Enter second word: stumble The words are not anagrams. Procedure: Step 1. Create the method: void countLetters (String word, int[] counts) The variable counts is an array of 26 integers used to keep track of how many times each letter appears in word. Each element in counts holds the count for a single letter. Element O holds the count for the letter 'A', Element 1 holds the count for the letter 'B' etc. For example, after the word smartest has been read, the array should contain the values 1000 10000000 10000 122000000, smartest contains one a, one e, one m, one r, two s's and two t's. To find which index you should increment for a letter: 1. convert the letter to UPPERCASE 2. find the index of that letter in a class constant that contains every uppercase letter of the alphabet i.e. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 3. increment the element in counts using that index. Do the above in a loop until you have counted the appearance of every letter in word To test this method, create an array of 26 ints, and pass in it and nonsense words like "abbccc". When the method returns, use the printArray method you created last class to check if the array has the correct values. Step 2 In main create two arrays to hold letter counts for two words. (If you are going to reuse the array that you created in Step 1, remember that it will contain numbers from your testing so comment out that test code). Prompt the user for two words. Then call countLetters twice, once for each of the words entered by the user. Check if the two arrays holding the letter counts are the same. If they are, print a message saying the words are anagrams. If they are not, print a message saying they are notStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started