-1

im a first year programmer and i was wondering what this code extract does. Its part of a main which does a check to see if 2 words are anagrams of one another. Im not fullly sure what this represents though.

    for(int x = 0; x < array.length; x++)
    {
        if(x == array.length-1)
        {
            array[x] = word1.substring(x);
        }
        else
        {
            array[x] = word1.substring(x, x+1);
        }
    }       
    for(int x = 0; x < array.length && isAnagram != false; x++)
    {
        aLetter = array[x];
        if(word2.indexOf(aLetter) != -1)
        {
            isAnagram = true;
        }
        else
        {
            isAnagram = false;
        }
        if(isAnagram == true)
        {
            word2 = word2.replaceFirst(aLetter, "");
        }
    }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Chilli
  • 603
  • 1
  • 6
  • 8

1 Answers1

0

Loop 1:

Make array of letters in word1.

Loop 2:

Remove each letter from word2.
isAnagram starts true.
If any letter is not found then isAnagram is false.

At the end isAnagram should be true and word2 should be empty.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
QuentinUK
  • 2,997
  • 21
  • 20