Advertisement

What's wrong with this?

Started by March 05, 2002 09:32 PM
17 comments, last by Draxis 22 years, 6 months ago
I whipped this string comparison function up in under a minute and it compiled, first try! So there must be SOMETHING wrong with it. IE it''s not portable or there''s uneeded stuff in it, etc. So, how can I make it better?
  
int compstring(const char *string1, const char *string2)
{
    int size1 = strlen(string1);
    int size2 = strlen(string2);
    for(int index = 0; string1[index] != ''\0'' && string2[index] != ''\0'' && index < size1 && index < size2; index++)
    {
        if(string1[index] == string2[index])
        {
            continue;
        }
        else
        {
            return 0;
        }
    }
    return 1;
}
  
Ok, so I didn''t need the strlen() stuff, since the ''\0'' condition takes care of that... So now I have
  int compstring(const char *string1, const char *string2){    for(int index = 0; string1[index] != ''\0'' && string2[index] != ''\0''; index++)    {        if(string1[index] == string2[index])        {            continue;        }        else        {            return 0;        }    }    return 1;}  

How''s this look?
Advertisement
Even better...

      int compstring(const char *string1, const char *string2){    int limit = (strlen(string1)> strlen(string2)) ? strlen(string2) : strlen(string1);    for(int index = 0; index < limit; index++)        if(!(string1[index] == string2[index]))            return 0;    return 1;}  


//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

If you cut out the strlens, you need a final test to make sure the sizes are equal.

    int compstring(const char *string1, const char *string2){    for(int index = 0; string1[index] != '\0' && string2[index] != '\0'; index++)    {        if(string1[index] == string2[index])        {            continue;        }        else        {            return 0;        }    }    return (string1[index] == string2[index] ? 1 : 0);}    


Edited by - sneftel on March 5, 2002 10:42:55 PM
quote: Original post by ZealousElixir
Even better...

        int compstring(const char *string1, const char *string2){    int limit = (strlen(string1)> strlen(string2)) ? strlen(string2) : strlen(string1);    for(int index = 0; index < limit; index++)        if(!(string1[index] == string2[index]))            return 0;    return 1;}    




Ew... three calls to strlen. Don''t forget to consider the performance of standard functions; they tend to be very well optimized, but strlen will never go under best-case of O(n).
If you want to make sure the strings are the same length you could possibly speed it up with the following.

        int compstring(const char *string1, const char *string2){   int str1len, str2len;   str1len = strlen(string1);   str2len = strlen(string2);      if (str1len != str2len)    return 0;        for(int index = 0; index < str1len; index++)    {        if(string1[index] == string2[index])        {            continue;        }        else        {            return 0;        }    }        return 1;}        


Edit: Spelling and code errors.

Edited by - slepyii on March 5, 2002 10:51:02 PM
Advertisement
quote: Original post by slepyii
If you want to make sure the strings are the same length you could possible speed it up with the following.

        int compstring(const char *string1, const char *string2){    int str1len, str2len;   str1len = str1len(string1);   str2len = str2len(string2);      if (str1len != str2len)    return 0;        for(int index = 0; index < str1len; index++)    {        if(string1[index] == string2[index])        {            continue;        }        else        {            return 0;        }    }        return 1;}        



Again.... don''t assume strlen is magic. Each of those calls iterates through the entire string, a character at a time, looking for a null. Since you''re going to be iterating through yourself anyway, you may as well stop before you iterate all the way through.
I included the string length comparison condition, but I needed to alter it.
      int str1len, str2len;       str1len = strlen(string1);       str2len = (strlen(string2)) + 1;        if (str1len != str2len)    {        return 0;    }  

This is because when I call the function compstring(command, "stats"); "stats" doesn''t have a nul character, while command does, since it was aquired using fgets()... Or something like that.
I dunno
Oooo, ouch!
I just realized, I''d better tack a nul character onto that string, lest it renders my comparison condition useless.
quote: Original post by Sneftel
Ew... three calls to strlen. Don''t forget to consider the performance of standard functions; they tend to be very well optimized, but strlen will never go under best-case of O(n).


3 calls to strlen vs. 2 calls to strlen and two temp variables?

Maybe if you wanted to go all out, but the performance gain with strings of reasonable size is negligible, and the other code is a lot prettier. Also, if you''re using the methods that don''t require strlen at all, you''re doing three comparisons per loop iteration...a bit excessive.

Later,
ZE.



//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement