-2

I have made a simple function which checks if two words are an anagram by sorting and comparing the sorted values, however this program always returns true even if the words aren't anangrams. If I remove the .ToString() it evalutates as false. Any idea why it's doing this and any ideas on how to fix this?

public bool anagram(string word1, string word2)
    {
        char[] word1Arr = word1.ToArray();
        char[] word2Arr = word2.ToArray();

        Array.Sort(word1Arr);

        Array.Sort(word2Arr);

        Console.WriteLine(word1Arr);
        Console.WriteLine(word2Arr);

        if (word1Arr.ToString() == word2Arr.ToString())
        {
            return true;
        }
        else
        {
            return false;
        }


    }
Ay Jay
  • 45
  • 4
  • 7
    Have you checked what the output of `word1Arr.ToString()` is? – user247702 Jul 24 '18 at 12:29
  • 1
    It would be awesome if you could provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). In particular of sample of your data would be helpful. – MakePeaceGreatAgain Jul 24 '18 at 12:30
  • 1
    word1Arr.ToString() will print something like "char[]". So it's always true because both valeus are char[]. Try using SequenceEqual (https://msdn.microsoft.com/en-us/library/bb348567(v=vs.110).aspx) – Afonso Jul 24 '18 at 12:31
  • 1
    To create a string from a char array use `new String(..)` not `.ToString()`. BTW debugging and checking the values in each step would have shown what's wrong – Panagiotis Kanavos Jul 24 '18 at 12:33
  • 2
    @AyJay debugging and stepping into the code is even easier – Panagiotis Kanavos Jul 24 '18 at 12:34
  • @PanagiotisKanavos I did step into the code all it did was return the char array which should have returned false because they weren't the same. This was a deeper underlying issue with how the functions are implemented on the lower level – Ay Jay Jul 24 '18 at 12:39
  • 2
    @AyJay there's no deeper underlying issue. You *didn't* check what was being compared otherwise you'd have seen it's `"System.Char[]"` – Panagiotis Kanavos Jul 24 '18 at 12:41
  • 2
    @AyJay if you did debug it, you would notice `word1Arr.ToString()` does not return what you think it returns. – Euphoric Jul 24 '18 at 12:41
  • Instead of comparing one `word1Arr.ToString() ` against the other, save the results into a variable first. This makes the code cleaner and easier to debug. `word1Arr.ToString()` will generate a temporary string anyway, might as well store it somewhere – Panagiotis Kanavos Jul 24 '18 at 12:42
  • 1
    @AyJay you could also add both expressions as watch variables. That would also show that something was wrong – Panagiotis Kanavos Jul 24 '18 at 12:43
  • @PanagiotisKanavos Thank you for actually teaching me alternative ways to debug and showing me my mistakes. I'll read more into it – Ay Jay Jul 24 '18 at 12:44

2 Answers2

8

replace

if (word1Arr.ToString() == word2Arr.ToString())

with

if (word1Arr.SequenceEqual(word2Arr))

to compare the content of the arrays - because word1Arr.ToString() returns "System.Char[]" and your comparison results into

"System.Char[]" == "System.Char[]" 

is always true

fubo
  • 44,811
  • 17
  • 103
  • 137
2

You can also use this comparison:

if(new string(word1Arr) == new string (word2Arr))

ToString method has default implementation in char[] returning name of a type System.Char[], that's why you are getting true when you compare two char[].

Comparing to the other answer: my method created two extra instances of string class, while the other answer compares arrays without creating extra objects.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69