An anagram of a word is a permutation of the letters in that

An anagram of a word is a permutation of the letters in that word for example, “stop” is an anagram of “tops”.

•Input


•The input to the program is a list of words read from an input file, the name of the file must be typed from keyboard by users.
•Output


•On screen: The output is a list containing the same words, but with anagrams displayed on the same line and should be displayed both on the screen as well as an output file name output.txt.


•On file: This output file name must be built into the program.

------------------------------------------------------------------------------------------------------------------------------------------------------------

Write a Java program that finds anagrams

•String operations
•Input and Output Text skills

REQUIREMENTS

•When determining if two words are anagrams, the program must treat upper and lower case letters as equivalent (thus “Pans” and “snap” are anagrams) and ignore punctuation marks (“it’s” and “Sit” are anagrams). However, the program must display words with their original capitalization and punctuation – as shown above on the screen.


•2. The “word” is assumed to be any series of nonblank characters words may be separated by any number of whitespace characters. Any number of words may appear on a line, including none.


•3. The program must work even if the input file is empty. If this is the case print a message saying that “the input file is empty” and then terminate the program.


•4. if a word has no anagram, print it in a single line

Solution

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AnagramChecker {

   public static boolean checkAnagram(String a, String b)
   {
       if(a == b)
       {
           return true;
       }
       if(a == b)
       {
           return true;
       }
       char[] aArr = a.replaceAll(\"[^a-zA-Z]\", \"\").toLowerCase().toCharArray();
       char[] bArr = b.replaceAll(\"[^a-zA-Z]\", \"\").toLowerCase().toCharArray();
      
       if (aArr.length != bArr.length)
           return false;
      
       // An array to hold the number of occurrences of each character
       int[] counts = new int[26];
      
       for (int i = 0; i < aArr.length; i++)
       {
           counts[aArr[i]-97]++; // Increment the count of the character at respective position
           counts[bArr[i]-97]--; // Decrement the count of the character at respective position
       }

       // If the strings are anagrams, then counts array will be full of zeros not otherwise
       for (int i = 0; i<26; i++)
       {
           if (counts[i] != 0)
               return false;
       }
      
       return true;
   }
  
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);
       System.out.print(\"Enter file name: \");
       String fileName= sc.next();
       sc.close();
      
       BufferedReader br = null;
       FileReader fr = null;

       List<String> stringList = new ArrayList<>();
       try {
           String sCurrentLine;
           br = new BufferedReader(new FileReader(fileName));
           while ((sCurrentLine = br.readLine()) != null)
           {
               stringList.add(sCurrentLine);
           }
       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
       finally
       {
           try {
               if (br != null)
                   br.close();
               if (fr != null)
                   fr.close();
           }
           catch (IOException ex)
           {
               ex.printStackTrace();
           }
       }
      
       if (stringList.isEmpty())
       {
           System.out.println(\"the input file is empty\");
           return;
       }
      
       try
       {
       List<String> anagrams = new ArrayList<>();
       for(int i = 0; i < stringList.size(); i++)
       {
           String st1 = stringList.get(i);
           stringList.set(i, \"\");
           if (st1.isEmpty())
               continue;
           StringBuilder str = new StringBuilder();
           str.append(st1);
           for (int j = i+1; j < stringList.size(); j++)
           {
               String st2 = stringList.get(j);
               if (checkAnagram(st1, st2))
               {
                   stringList.set(j, \"\");
                   str.append(\" \").append(st2);
               }
           }
           anagrams.add(str.toString());
       }
      
       PrintWriter writer = new PrintWriter(\"output.txt\", \"UTF-8\");
       for(String anagram: anagrams)
       {
           System.out.println(\"anagrams: \" + anagram);
           writer.write(anagram);
       }
       writer.close();
       }
       catch (IOException e)
       {
           System.out.println(e);
       }
   }
}

/*

Sample run

input.txt file

abbcdefgh oplo
olabbcd efgh po
nsadhsadsa
mkloi
ilokm

Enter file name: input.txt
anagrams: abbcdefgh oplo olabbcd efgh po
anagrams: nsadhsadsa
anagrams: mkloi ilokm

*/

An anagram of a word is a permutation of the letters in that word for example, “stop” is an anagram of “tops”. •Input •The input to the program is a list of wor
An anagram of a word is a permutation of the letters in that word for example, “stop” is an anagram of “tops”. •Input •The input to the program is a list of wor
An anagram of a word is a permutation of the letters in that word for example, “stop” is an anagram of “tops”. •Input •The input to the program is a list of wor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site