简体   繁体   中英

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);
      }

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);
            }
        }

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'

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) +"%");

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM