944,111 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 7950
  • C RSS
0

strcmp implementation from scratch

by on May 22nd, 2009
This is how mine strcmp function would look like if I'd to implement it from scratch
C Code Snippet (Toggle Plain Text)
  1. /**
  2. @author: Mathias Van Malderen (tux4life)
  3. */
  4.  
  5. int strcmp(const char *s1, const char *s2)
  6. {
  7. while((*s1 && *s2) && (*s1++ == *s2++));
  8. return *(--s1) - *(--s2);
  9. }
Comments on this Code Snippet
May 28th, 2009
0

Re: strcmp implementation from scratch

Yet to mention: I didn't include a NULL-pointer check
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 15th, 2010
-1

Re: strcmp implementation from scratch

your solution doesn't deal with the case that one string is the prefix of the other.
Newbie Poster
agamiro is offline Offline
1 posts
since Mar 2010
Mar 15th, 2010
1

Re: strcmp implementation from scratch

>>return *(--s1) - *(--s2);
That may give you the wrong result. It should be return *s1 - *s2; There are four possible return values
  1. If *s1 is '\0' and *s2 is not, then the return value will be some negative value.
  2. If *s1 is NOT '\0' but s2 is, then the return value will be some positive value
  3. If both *s1 and *s2 are '\0' then the return result will be 0.
  4. If neither *s1 nor *s2 is '\0' then the return value could be either positive or negative, depending on their values.

In either event, decrementing s1 and s2 before the subtraction may (and most probably will) produce the wrong result because s1 and s2 will not be pointer to the characters that cause the previous loop to terminate.
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Mar 18th, 2010
0

Re: strcmp implementation from scratch

@Ancient Dragon:
Thanks for pointing out this issue, I didn't notice it when I was writing the code. However, the solution you suggest (unfortunately) won't fix the problem, since it will create another one.
If I replace return *(--s1) - *(--s2); by return *s1 - *s2; ,
then strcmp("a", "b") would yield the result 0, and not -1, because both s1 and s2 are incremented by one in the while's expression, s1 and s2 will point to the
nul-terminators by the time the return statement is encountered, and thus
return *s1 - *s2; will result in returning 0.
Last edited by tux4life; Mar 18th, 2010 at 4:02 pm.
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 18th, 2010
3

Re: strcmp implementation from scratch

correction
  1. int mstrcmp(const char *s1, const char *s2)
  2. {
  3. while((*s1 && *s2) && (*s1 == *s2))
  4. s1++,s2++;
  5. return *s1 - *s2;
  6. }
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Mar 19th, 2010
0

Re: strcmp implementation from scratch

Here's a better idea for the return value: return an explicit value instead of allowing the possibility of signed integer overflow?
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 20th, 2010
0

Re: strcmp implementation from scratch

@Dave: how can *s1 - *s2 cause an integer overflow? The maximum values would be -255 (0 - 255). That's hardly even close to an integer overflow value.
Last edited by Ancient Dragon; Mar 20th, 2010 at 12:59 am.
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Mar 20th, 2010
0

Re: strcmp implementation from scratch

@AD: I'll leave that as an exercise for the programmer.

You don't really like portable programming or the design process involved, do you? If it happens to run on a particular compiler at a given moment in time, it appears "correct" to you?

Do you remember that qsort comparison function with the integer overflow issue?



No. I don't wantto continue. You are the lord of this roost. I am exhausted trying to get people to write portable code -- especially since you always come along and tell people to write implementation-specific stuff. Again, this is why I think of you as Daniweb's Herb Schildt. But he is a good author who has influenced many C and C++ programmers. It just sucks that it took him like 10 years to clean up his code. I think I need a break from all of this stupid bickering. Please take some time to wander into c.l.c and learn some new things.
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 20th, 2010
0

Re: strcmp implementation from scratch

@Dave: I didn't quite get you when you said:
Quote ...
Here's a better idea for the return value: return an explicit value instead of allowing the possibility of signed integer overflow?
Also, you seem to do exactly the same as I and AD did in your code snippet:
http://www.daniweb.com/code/snippet216567.html .
So, could you please clarify what you meant with returning an explicit value to avoid the possibility of a signed integer overflow ?

[EDIT]
@Dave:
Do you perhaps mean: return (int) *s1 - (int) *s2; ?
[/EDIT]
Last edited by tux4life; Mar 20th, 2010 at 12:58 pm.
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Message:
Previous Thread in C Forum Timeline: randomize function and boudery help
Next Thread in C Forum Timeline: void main vs int main





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC