2
votes

These are the details of my project:
Anagrams: The aim of this project is to create a game in which the user is presented with an anagram of a word and has to guess the right word within a limited number of attempts. Features of the Project:

  1. The user is given a fixed number of attempts to guess the correct word. The number of attempts is dependent on the length of the word.
  2. After each incorrect attempt the user is provided with a hint of the correct word.
  3. If the user is not able to guess the right word within the fixed number of attempts the correct word is displayed and the game moves on to the next word.
  4. Give controls for exiting the game.

Problems I am facing:

I was able to take a random word from an array of strings but wasn't able to compare that to the output as the output is a normal string. I would like to know how to fix this and also how should I proceed further, I don't need the answer for the hint part just want to know how can I compare 2 types of strings.

import java.util.Arrays;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.*;
import java.util.Scanner;
import java.util.*;

public class main {
    String A = "words[index]";

    static boolean isAnagram(String A, String B) {
        if (A.length() != B.length()) {
            return false;
        } else {
            char a[] = A.toLowerCase().toCharArray();
            char b[] = A.toLowerCase().toCharArray();

            Arrays.sort(a);
            Arrays.sort(b);
            String sortedA = String.valueOf(a);
            String sortedB = String.valueOf(B);
            if (sortedA.equals(sortedB)) {
            }
        }
        return false;
    }

    public static void main(String[] args) {
        String[] words = {"Rat", "Car", "Below", "Taste", "Cried", "Study", "Thing", "Chin"};
        Random random = new Random();
        int index = random.nextInt(words.length);
        System.out.println("The given word is: " + words[index]);

        Scanner sc = new Scanner(System.in);
        String B = sc.next();
        if (isAnagram(String A, String B)) {
            System.out.println("not an angram");
        } else {
            System.out.println("Sucess");
        }
    }
}
3

3 Answers

1
votes

You can get two arrays of characters from these strings, sort them, and then check if these arrays are equal to each other.

/**
 * @param a first string.
 * @param b second string.
 * @return whether two strings are case insensitive anagrams of each other.
 */
static boolean isAnagram(String a, String b) {
    // invalid incoming data
    if (a == null || b == null
            || a.length() != b.length()) return false;
    return Arrays.equals(
            a.toLowerCase().codePoints().sorted().toArray(),
            b.toLowerCase().codePoints().sorted().toArray());
}
public static void main(String[] args) {
    System.out.println(isAnagram("Study", "dusty")); // true
    System.out.println(isAnagram("door", "rooD"));   // true
    System.out.println(isAnagram("door", "root"));   // false
}

See also: Java 8 Stream function to group a List of anagrams into a Map of Lists

0
votes

I can offer two methods for checking whether two strings are anagrams:

1.

static boolean isAnagram(String A, String B) {
    if (A.length() != B.length()) return false;
    else {
        char[] a = A.toLowerCase().toCharArray();
        char[] b = A.toLowerCase().toCharArray();
        Arrays.sort(a);
        Arrays.sort(b);
        for (int i = 0; i < a.length; i++)
            if (a[i] != b[i]) return false;
        return true; //time Complexity: O(nLogn)
    }
}
static int NUM_OF_CHARS = 256;

static boolean isAnagram(String A, String B) {
    if (A.length() != B.length()) return false;
    else {
        int[] count1 = new int[NUM_OF_CHARS];
        Arrays.fill(count1, 0);
        int[] count2 = new int[NUM_OF_CHARS];
        Arrays.fill(count2, 0);
        char[] a = A.toCharArray();
        char[] b = A.toCharArray();
        for (int i = 0; i < a.length && i < b.length; i++) {
            count1[a[i]]++;
            count2[b[i]]++;
        }
        for (int i = 0; i < NUM_OF_CHARS; i++)
            if (count1[i] != count2[i]) return false;
        return true;
    }//time Complexity: O(n)
}
0
votes

Check whether two strings are anagram or not using collections:

package StringPackage;
import java.util.*;

public class AnagramUsingCollections {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char[] string1 = "aab".toCharArray();
        char[] string2 = "baa".toCharArray();

        Map<Character, Integer> hm = new HashMap<>();
        for (char no : string1) {
            //for start
            Integer count = hm.get(no);
            if (count == null) {
                hm.put(no, 1);
            } else {
                count++;
                hm.put(no, count);
            }
            //for end
        }
        if (string1.length == string2.length) {
            for (char no : string2) {
                //baa
                Integer count = hm.get(no);
                if (count == null) {
                    hm.put(no, 1);
                } else {
                    count--;
                    hm.put(no, count);
                }
            }//for end
        }
        Set<Map.Entry<Character, Integer>> se = hm.entrySet();
        for (Map.Entry<Character, Integer> me : se) {
            if (me.getValue() > 0) {
                System.out.println("Not an anagram");
                break;
            } else {
                System.out.println("IS an anagram");
                break;
            }
        }
    }
}