Augustas
Augustas

Reputation: 23

How to sort chararrays manually with a loop?

I need to check if two strings are anagrams but I cant use arrays.sort ... I know that I have to use for loop but I do not know how to start so that the chararray would be sorted alphabetically. Please, help me.

import java.util.Scanner;
public class Assignement3{
public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println( "Please, type the first word: ");
    String word1=sc.nextLine();
    System.out.println( "Please, type the second word: ");
    String word2=sc.nextLine();
    String word1lower=word1.toLowerCase().replace(" ","");
    String word2lower=word2.toLowerCase().replace(" ","");
    System.out.println("Your First word is: " + word1lower);
    System.out.println("Your Second word is: " + word2lower);
    char[] firstword=word1lower.toCharArray();
    char[] secondword=word2lower.toCharArray();
  }
  }`

Upvotes: 0

Views: 60

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

I think that providing code which determines whether two strings are meaningful or actual anagrams of each other would require doing a lookup in a dictionary. But if we define an anagram of a word as being some permutation of the existing characters in the original word, then we can check for this fairly easily.

In the code snippet below, I read the characters from the first word into a map, keeping counts of the occurrences of each letter. This map of characters represents everything which is available to form a potential anagram. By iterating over the second word and keeping track of each character consumed, we can tell if the second word be an anagram. The marker for failure would be trying to use a character which does not appear or which has been exhausted already. Otherwise, the second word is a potential anagram.

String word1lower = "hala babel";
String word2lower = "baha label";
char[] firstword = word1lower.toCharArray();
char[] secondword = word2lower.toCharArray();

Map<Character, Integer> m1 = new HashMap<>();
int count = 0;
for (char c : firstword) {
    Integer cnt = m1.get(c);
    m1.put(c, cnt == null ? 1 : cnt.intValue() + 1);
    ++count;
}
boolean isAnagram = true;
for (char c : secondword) {
    Integer cnt = m1.get(c);
    if (cnt == null || cnt.intValue() == 0) {
        isAnagram = false;
        break;
    }
    m1.put(c, cnt.intValue() - 1);
    --count;
}

if (isAnagram && count == 0) {
    System.out.println("Second word is a full anagram of the first word.");
}
else if (isAnagram) {
    System.out.println("Second word is a partial anagram of the first word.");
}
else {
    System.out.println("Second word is not an anagram of the first word.");
}

Again, I say potential anagram here because to check whether a random combination of characters corresponds to an actual English (or other language) word would require a dictionary. This is way past the scope of a single Stack Overflow question, but hopefully my answer gets you thinking in the right direction.

Demo here:

Rextester

Upvotes: 1

Related Questions