Get Percentage of matching two string

write this code in main method or as per you need
==============================================================
CalcSimilarity("romit.mewada", "RomitMewada", true)
==============================================================



private static int CalcLevensteinDistance(string left, string right)
{
    if (left == right)
    return 0;
    int[,] matrix = new int[left.LeMngth + 1, right.Length + 1];
    for (int i = 0; i <= left.Length; i++)
    // delete
    matrix[i, 0] = i;
    for (int j = 0; j <= right.Length; j++)
    // insert
    matrix[0, j] = j;
    for (int i = 0; i <>
    {
        for (int j = 0; j <>
        {
            if (left[i] == right[j])
                matrix[i + 1, j + 1] = matrix[i, j];
            else
            {
                // deletion or insertion
                matrix[i + 1, j + 1] = System.Math.Min(matrix[i, j + 1] + 1, matrix[i + 1, j] + 1);
 
                // substitution
                matrix[i + 1, j + 1] = System.Math.Min(matrix[i + 1, j + 1], matrix[i, j] + 1);
            }
        }
    }
    return matrix[left.Length, right.Length];
}
public static double CalcSimilarity(string left, string right, bool ignoreCase)
{
    if (ignoreCase)
    {
        left = left.ToLower();
        right = right.ToLower();
    }
    double distance = CalcLevensteinDistance(left, right);
    if (distance == 0.0f)
    return 1.0f;
    double longestStringSize = System.Math.Max(left.Length, right.Length);
    double percent = distance / longestStringSize;
    return 1.0f - percent;
}

No comments: