Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An anagram is a word formed by rearranging the letters of another word. For this

ID: 3743325 • Letter: A

Question

An anagram is a word formed by rearranging the letters of another word. For this program, we will extend that definition to include any words formed by rearranging any subsetsof letters of another word. Write a program that takes two arguments: a length n and a string, and outputs all english words of length n that appear in the given string. For example, if the

n= 4 andthe string is “caret”, the output would be similar to the following:

1 n = 4, word=Caret

2 acer

3 acre

4 aret

5 care

6 cart

7 cate

8 cert

9 race

10 rate

11 rect

12 tace

13 tare

14 tear

The input should accept both upper-case and lower-case letters in the string, and should

ignore the case when finding the anagrams.

in JAVA

They have given me the dictionary text file already

an example of the test file is: A
AA
AAA
Aachen
aah
aahed
aahing
aahs
aal
Aalborg
Aalesund
aalii
aaliis
Aaliyah
Aaliyah's
aals
Aalst
Aalto
AAM
Aarau
aardvark
aardvark's
aardwolf
aardwolf's
aardwolves
Aargau
aargh
Aarhus
Aaron
Aaronic

I cant post it all because the file is too large.

Explanation / Answer

import java.io.*; import java.util.*; public class Anagrams { public static List<String> dictionary = new ArrayList<String>(); public static void anagrams(String word, int requiredLen) { anagrams("", word.toUpperCase(), requiredLen); } private static void anagrams(String prefix, String suffix, int requiredLen) { // base case, it will terminate, when there is no suffix remaining int n = suffix.length(); if (n == 0) { // prefix is a anagram now if(dictionary.contains(prefix) && prefix.length() == requiredLen) { System.out.println(prefix); } } else { // append each character of suffix one by one to prefix for (int i = 0; i < n; i++) anagrams(prefix + suffix.charAt(i), suffix.substring(0, i) + suffix.substring(i+1, n), requiredLen); } } public static void main(String[] args) { Scanner scan = null; try { scan = new Scanner(new File("./dictionary.txt")); while (scan.hasNextLine()) { dictionary.add(scan.nextLine().toUpperCase()); } } catch (FileNotFoundException e) { System.out.println("File not found"); } Scanner keyboard = new Scanner(System.in); // take the word and length of anagram System.out.println("Enter the original word: "); String anagramString = keyboard.next(); System.out.println("Enter anagram word length: "); int length = keyboard.nextInt(); anagrams(anagramString, length); keyboard.close(); } }

i think above code should help you, here i am trying to read a file "dictionary.txt" which is placed in classpath. After that
i am asking user for the word whose anagrams need to be find and then the required length of the words.
Please do give it a shot. In case of any issues, please share your dictionary file in gdrive and i will surely help.

Thnaks!

If the answer helps you, please upvote. I am in great need of upvotes. Thanks!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote