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

5
  • How about this? Commented Jul 15, 2020 at 12:06
  • 2
    What result of the comparison you're looking for? Commented Jul 15, 2020 at 12:16
  • DUPLICATION: stackoverflow.com/questions/3343874/… Commented Jul 15, 2020 at 12:58
  • Does this answer your question? Compare two strings and get the difference Commented Jul 15, 2020 at 13:04
  • Doing a Diff or even knowing what you want is not a trivial thing.. Commented Jul 15, 2020 at 14:50

2 Answers 2

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];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

What if the same character appears twice in the same string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.