1

I am new to C#. I have 2 strings, they are representing characters from International Phonetic Alphabet.

String 1 - ðə ɻɛd fɑks ɪz hʌŋgɻi 
String 2 - ðæt ɪt foks ɪn ðʌ sʌn ɻe͡i 

Now I need to compare String 1 with String 2 and find how much String 2 differ from String 1. I need this value as a percentage value. How can I do this? Small code example will help me a lot. Your help will be greatly appreciated.

KingCronus
  • 4,509
  • 1
  • 24
  • 49
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • A `function` that iterate over the `string` characters and returns how many are equal ? – Ofiris May 30 '13 at 15:51
  • 5
    percentage of what? String length? Exact character matches? please specify the rule! and most of all, **[What have you tried?](http://whathaveyoutried.com)** – Steve B May 30 '13 at 15:51
  • 1
    Take a look at [this question](http://stackoverflow.com/questions/5444945/how-to-check-if-two-string-are-a-partial-match-in-c?rq=1) and [this question](http://stackoverflow.com/questions/15398730/how-can-i-get-a-percent-accuracy-match-when-comparing-two-strings-of-an-address) – jszigeti May 30 '13 at 16:03
  • 1
    @jszigeti, if you think this is a duplicate you should flag it as such using the flag button under the question. – Liam May 30 '13 at 16:15
  • @SteveB: Exact character matches – PeakGen May 30 '13 at 16:59
  • @SteveB: No need to point out "What you have tried". I must know the perfect system before I start going through char by char inside a loop, finding the exact match, and giving it a value – PeakGen May 30 '13 at 17:04

2 Answers2

9

You should have tell what is your String Metric

Also, have a look in this How to find difference between two strings - C# question.

This will compare char by char, it is different than Llevenshtein Distance which is more common when comparing string differences.

void Main()
{
    string str1 = "ðə ɻɛd fɑks ɪz hʌŋgɻi";
    string str2 = "ðæt ɪt foks ɪn ðʌ sʌn ɻe͡i";
    Console.WriteLine(StringCompare(str1,str2)); //34.6153846153846
    Console.WriteLine(StringCompare("same","same")); //100
    Console.WriteLine(StringCompare("","")); //100
    Console.WriteLine(StringCompare("","abcd")); //0  
}

static double StringCompare(string a, string b)
{
    if (a == b) //Same string, no iteration needed.
        return 100;
    if ((a.Length == 0) || (b.Length == 0)) //One is empty, second is not
    {
        return 0;
    }
    double maxLen = a.Length > b.Length ? a.Length: b.Length;
    int minLen = a.Length < b.Length ? a.Length: b.Length;
    int sameCharAtIndex = 0;
    for (int i = 0; i < minLen; i++) //Compare char by char
    {
        if (a[i] == b[i])
        {
            sameCharAtIndex++;
        }
    }
    return sameCharAtIndex / maxLen * 100;
}
Community
  • 1
  • 1
Ofiris
  • 6,047
  • 6
  • 35
  • 58
  • 2
    +1 for Levenshtein distance, love learning new things! – Liam May 30 '13 at 16:14
  • hmm, this is interesting. In your example, you are checking the length and char by char as well right? – PeakGen May 30 '13 at 17:05
  • In your code how can b.length ever equal zero if the strings don't equal each other? – Sayse May 30 '13 at 17:23
  • 1
    @Sayse, you are right, I added the equal test afterwards.. Thanks, improved it now. – Ofiris May 30 '13 at 18:23
  • @Knight, the function just go char by char and returns percentage of equal chars. I check if strings are equal for efficiency in case of 100%. – Ofiris May 30 '13 at 18:27
0

Something along the lines of

string str1 = "ðə ɻɛd fɑks ɪz hʌŋgɻi";
string str2 = "ðæt ɪt foks ɪn ðʌ sʌn ɻe͡i";

char[] str1Char = str1.ToCharArray();
char[] str2Char = str2.ToCharArray();

int matchCount = 0;
int unmatchedCount = 0;


for (int x = 0; x>= str1Char.Length || x >= str2Char; x++;)
{
   if (str1Char[x] == str2Char[x])
      matchCount++;
   else
      unmatchedCount++;
}

int longestString = str1Char.Length;
if (str2Char.Length > longestString )
    longestString = str2Char.Length;

int perc = (matchCount/longestString )*100;

this will fall over if the first string is shorter than the second but you get the jist

Liam
  • 27,717
  • 28
  • 128
  • 190