Ayman
Ayman

Reputation: 23

How to compare two char arrays and display the different char?

I want to compare two char arrays and I want (if they are not equal) the different char to be desplayed on the output. For exemple, in my code below, the different char is "y". So on the output, I want to be written: "the different char is y, it is in the array ay but not in the ar". I am working with C# and here is the code that I have wrote, I am getting an error with the CompareTo method. Can anyone help me ? Thanks in advance. NB, I am still a beginner.

     char[] ar = { 'a', 'h', 'm', 'a', 'd' };
     char[] ay = { 'a', 'y', 'm', 'a', 'd' };
     if (ar.SequenceEqual(ay))
     {
        Console.WriteLine("Success");
     }
     else
     {
        Console.WriteLine("failure");
        char diff = ar.CompareTo((Object)ay));
        Console.Write(diff);
      }

Upvotes: 0

Views: 1862

Answers (4)

meJustAndrew
meJustAndrew

Reputation: 6613

Using Except method you can achieve this pretty simple, and you don't have to compare the arrays twice.

char[] ar = { 'a', 'h', 'm', 'a', 'd' };
char[] ay = { 'a', 'y', 'm', 'a', 'd' };
var different = ar.Except(ay).ToList();
if (different.Count == 0)
{
         Console.WriteLine("Success");
}
else
{
         foreach (char c in different)
         {
               Console.WriteLine(c);
         }
}
Console.WriteLine("Percent of similarity: "+(100-different.Count*100/ar.Length) +"%");

Upvotes: 0

Sami
Sami

Reputation: 3800

Ayman, Except can do trick for you. here is the code.

IEnumerable<char> inArArray = ar.Except(ay);
foreach (char c in inArArray) {
             Console.WriteLine("{0} Exists in ar but not in ay",c);
}


IEnumerable<char> inAyrray = ay.Except(ar);
foreach (char c in inAyrray)
{
                Console.WriteLine("{0} Exists in ay but not in ar", c);
}

Upvotes: 0

Alex W
Alex W

Reputation: 505

Try this option, I also added an extra char to the array just for the failure check:

using System;
using System.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] ar = { 'a', 'h', 'm', 'a', 'd' };
            char[] ay = { 'a', 'y', 'f', 'm', 'a', 'd' };
            Console.WriteLine(GetDiffOfCharArray(ay, ar, "ay", "ar"));
            ay = new char[] { 'a', 'h', 'm', 'a', 'd' };
            Console.WriteLine(GetDiffOfCharArray(ay, ar, "ay", "ar"));
            Console.ReadLine();
        }

        private static string GetDiffOfCharArray(char[] array1Values, char[] array2Values, string array1Name, string array2Name)
        {
            string result = "";

            if (array1Values.SequenceEqual(array2Values))
            {
                result = string.Format("Success, Array '{0}' matches Array '{1}'", array1Name, array2Name);
            }
            else
            {
                var diff = array1Values.Except(array2Values);
                result = string.Format("Failure, the different char is '{0}', it is in the array '{1}' but not in the array '{2}'", 
                    string.Join("','", diff), 
                    array1Name, 
                    array1Name);
            }
            return result;
        }
    }
}

Where the output for failure and success are as follows:

Failure, the different char is 'y','f', it is in the array 'ay' but not in the array 'ay

Success, Array 'ay' matches Array 'ar'

Upvotes: 0

Alex Diamond
Alex Diamond

Reputation: 586

Take a look at this;

        char[] ar = { 'a', 'h', 'm', 'a', 'd' };
        char[] ay = { 'a', 'y', 'm', 'a', 'd' };
        List<char> matches = new List<char>();

        if (ar.SequenceEqual(ay))
        {
            Console.WriteLine("Success");
        }
        else
        {
            for (int i = 0; i < ar.Count(); i++)
            {
                if (ar[i] != ay[i])
                {
                    matches.Add(ay[i]);
                }
            }
            foreach (var matched in matches)
            {
                Console.WriteLine("Failure: " + matched);
            }
        }

Upvotes: 2

Related Questions