-2

I need an algorithm of sorts that will return a comparison value that can be used regardless of the string's length. I have tried a few different methods to this, but quite frankly, I suck at maths.

Basically I need to get a value that finds the percentage of different characters in a string and return them to a value that can be used against a threshold.

Here are some examples:

A Sample String -> A Samlpe String

2 character missing..

A Substantially Longer Sample String - > A Substantially Longer Samlpe String

still 2 character missing..

So how could I make something that returns a similar value, regardless of string length?

Cruxial
  • 11
  • 2
  • 8
  • What is the reasoning behind saying that there is 1 character missing? It looks to me like the difference between them is two characters, not one. – JLRishe Jan 14 '21 at 10:10
  • 1
    You're looking for the concept of [Edit Distance](https://en.wikipedia.org/wiki/Edit_distance) – Damien_The_Unbeliever Jan 14 '21 at 10:10
  • 1
    Please, take a look at this article: https://www.csharpstar.com/csharp-string-distance-algorithm/ – Dave Miller Jan 14 '21 at 10:13
  • Maybe you need something like https://stackoverflow.com/questions/6944056/c-sharp-compare-string-similarity – Dmitry Kolchev Jan 14 '21 at 10:15
  • see also [Fuzzy Text Matching C#](https://stackoverflow.com/q/8218553/995714), [Are there any Fuzzy Search or String Similarity Functions libraries written for C#?](https://stackoverflow.com/q/83777/995714) – phuclv Jan 14 '21 at 10:19

1 Answers1

0

I do not really understand the question, but is this what you mean?

public double Compare(string a, string b)
{
    int min = Math.Min(a.Length, b.Length);
    int max = Math.Max(a.Length, b.Length);

    double match = 0;
        
    for (int i = 0; i < min; i++)
    {
        if(a[i] == b[i])
        {
             match++;
        }
    }

    return match / max;
}

The string length doesn't matter here, you match the characters of the shortest string and return a percentage based on the longest string.

XLars
  • 177
  • 1
  • 5