tags:

views:

608

answers:

6

There are many ways one can implement the strcmp() function.
Note that strcmp(str1,str2) returns a negative number if str1 is alphabetically above str2, 0 if both are equal and postiveve if str2 is alphabetically above str1.

I want to implement it in c# without using any built in methods of .NET

In it can be implemented in C as follows:

int mystrcmp(const char *s1, const char *s2) 
{ 
    while (*s1==*s2) 
    { 
        if(*s1=='\0') 
           return(0); 
        s1++; 
        s2++; 
    } 
    return(*s1-*s2); 
}
A: 

Write it as you would in C, but using array subscript notation, instead of pointer notation.

Increment the index.

leppie
I got that...but I am confused on how to do alphabetically checking
Learner
+4  A: 

To avoid using any of the methods available in .NET or the BCL, you'd have to avoid the Length property of string (as properties are implemented by one or two methods). And you'd have to avoid the [] indexer property as well, for the same reason.

So you're pretty stuffed.

You make the assumption that the numeric character code indicates a human-significant sort ordering. It doesn't - the character codes in C# are Unicode, which has a lot of alphabets in it, some of which use a mixture of the Western alphabet (low values) with their own additional characters (high values).

So either you can reproduce a vast amount of character set information in your own code, so you know how to order two characters from Unicode, or you need to call a method in the BCL.

Daniel Earwicker
+2  A: 

one way could be like this. Code is edited based on comments...

public static int mystrcmp(string st1, string st2)
{
    int iST1 = 0, iST2=0;
    for (int i = 0; i < (st1.Length > st2.Length ? st1.Length : st2.Length); i++)
    {
        iST1 += (i >= st1.Length ? 0 : st1[i]) - (i >= st2.Length ? 0 : st2[i]);
        if (iST2 < 0)
        {
            if (iST1 < 0)
                iST2 += iST1;
            if (iST1 > 0)
                iST2 += -iST1;
        }
        else
        {
            iST2 += iST1;
        }
    }
    return iST2;
}
S M Kamran
how does this statement work 'st1[i] - st2[i];'....?
Learner
@Learner - you could write it exactly that way in C, it means the same thing. The only thing in the body of that function that isn't valid C is st1.length, which could be strlen(st1), assuming the strings become `const char *`.
Daniel Earwicker
got it ..that means st1[i] - st2[i]; will go an ASCII char value substraction....so it will give negative value for 'A' - 'B'.....
Learner
Character can be treated as numeric... so if A = 65 in ascii and B = 66 in ascii then A - B will yield +1....
S M Kamran
But this is only true if the country's character set is sequential within Unicode. It depends whether you want to cut your potential customer base by a large factor or not.
Daniel Earwicker
Yup but then the sub routine posted in Question will fail as well :)... There is no such thing like a perfect solution. A solution is only valid for a specified domain. :)
S M Kamran
@S M Kamran: actually, 'A' - 'B' is -1. :)
unwind
kamran: Your code wont work if the 2 strings are Test input 1: s1 = "ABCC" and s2= "ABC"Test input 2: s1=""ABC" and s2 = "ABCC"
Learner
@Unwind: Yes, thanks for the correction.
S M Kamran
@S M Kamran - There's no such thing as a perfect answer, but there's often a better answer (in this case: use the existing capabilities in the BCL).
Daniel Earwicker
@Earwicker - Yes, you are right if it's already in BCL then yes. However some pplz really do like to do things on their own...
S M Kamran
+2  A: 

Get a copy of .NET Reflector and inspect how the Compare()/CompareTo() methods of System.String and System.Globalization.CompareInfo are implemented in mscorlib.

VolkerK
+1  A: 

Don't use char* if you want to do this. Char* is unicode, you need ascii.

Your best bet would be using byte*. Then you can use the algorithm you currently have.

Dykam
+1  A: 

Calculate the Levenshtein distance between the two strings. and return that...

Here is a .net implementation of the Levenshtein distance from dot net Pearls:

using System;

/// <summary>
/// Contains approximate string matching
/// </summary>
static class LevenshteinDistance
{
    /// <summary>
    /// Compute the distance between two strings.
    /// </summary>
    /// <param name=s>The first of the two strings.</param>
    /// <param name=t>The second of the two strings.</param>
    /// <returns>The Levenshtein cost.</returns>
    public static int Compute(string s, string t)
    {
        int n = s.Length;
        int m = t.Length;
        int[,] d = new int[n + 1, m + 1];

        // Step 1
        if (n == 0)
        {
            return m;
        }

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

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

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

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

                // Step 6
                d[i, j] = Math.Min(
                    Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                    d[i - 1, j - 1] + cost);
            }
        }
        // Step 7
        return d[n, m];
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(LevenshteinDistance.Compute("aunt", "ant"));
        Console.WriteLine(LevenshteinDistance.Compute("Sam", "Samantha"));
        Console.WriteLine(LevenshteinDistance.Compute("flomax", "volmax"));
    }
}
Omar Kooheji