0

In C#, Im trying to compare two strings and find out how many chars are different.

I Tried this:

static void Main(String[] args)
{
    var strOne = "abcd";
    var strTwo = "bcd";

    var arrayOne = strOne.ToCharArray();
    var arrayTwo = strTwo.ToCharArray();

    var differentChars = arrayOne.Except(arrayTwo);

    foreach (var character in differentChars)
        Console.WriteLine(character);  //Will print a
}

but there are problems with this:

  1. if the strings contain same chars but on different positions within the string
  2. it removes the duplicate occurences

If the strings would be the same length I would compare the chars one by one but if one is bigger than the other the positions are different

canton7
  • 37,633
  • 3
  • 64
  • 77
  • How about [this](https://stackoverflow.com/a/18321594/5133585)? – Sweeper Jul 15 '20 at 12:06
  • 2
    What result of the comparison you're looking for? – Zohar Peled Jul 15 '20 at 12:16
  • DUPLICATION: https://stackoverflow.com/questions/3343874/compare-two-strings-and-get-the-difference – Ali Dehqan Jul 15 '20 at 12:58
  • Does this answer your question? [Compare two strings and get the difference](https://stackoverflow.com/questions/3343874/compare-two-strings-and-get-the-difference) – Ali Dehqan Jul 15 '20 at 13:04
  • Doing a [Diff](https://stackoverflow.com/questions/24887238/how-to-compare-two-rich-text-box-contents-and-highlight-the-characters-that-are/24970638?r=SearchResults&s=1|27.6912#24970638) or even knowing what you want is not a trivial thing.. – TaW Jul 15 '20 at 14:50

2 Answers2

1

How about this? Append two strings into one and group them, you will have the count of the chars, > 1 will give the repeatings and = 0 will give the unique ones.

var strOne = "abcd";
var strTwo = "bcd";

var arrayOne = strOne.Concat(strTwo).GroupBy(x => x).Select(x => new { Key = x.Key, Count = x.Count() });

foreach (var character in arrayOne) {
    if (character.Count > 1)
    {
        Console.WriteLine(character.Key); // the repeating chars
    }
}

If the same character appears twice in the same string,

 var strOne = "abbcdd";
 var strTwo = "cd";

 var arrayTwo = strOne.Select(x => new { Key = x, IsExists = strTwo.Any(y => y == x) });

 foreach (var character in arrayTwo) {
     if (character.IsExists)
     {
        Console.WriteLine(character.Key);
     }
 }
Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
1

You may want to have a look at the Leventshtein Distance Algorithm.

As the article says

the Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other

You can find many reference implementations in the internet, like here or here.

public static int LevenshteinDistance(string s, string t)
    {
        int n = s.Length;
        int m = t.Length;
        int[,] d = new int[n + 1, m + 1];

        if (n == 0)
        {
            return m;
        }

        if (m == 0)
        {
            return n;
        }

        for (int i = 0; i <= n; d[i, 0] = i++)
        {
        }

        for (int j = 0; j <= m; d[0, j] = j++)
        {
        }

        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

                d[i, j] = Math.Min(
                    Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                    d[i - 1, j - 1] + cost);
            }
        }
        return d[n, m];
    }
}
izlin
  • 2,129
  • 24
  • 30