DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Code Snippet: Another implementation of strcmp and strncmp functions (http://www.daniweb.com/forums/thread217385.html)

amrith92 May 25th, 2009 7:25 pm
Another implementation of strcmp and strncmp functions
 
My implementation of the
strcmp
and
strncmp
functions. I don't think that it is very efficient, but have managed to get both the functions' execution times to
0.01 s
(At least, this is the value my profiler gives me). Enjoy! :)

  1. /*
  2.   Function(Fn I) compares two strings and
  3.   returns (+1) if s1 > s2, (0) if s1 == s2,
  4.   and (-1) if s1 < s2
  5.  
  6.   Note: (all comparisns are ASCII based)
  7. */
  8. int aNew_strcmp(const char* s1, const char* s2)
  9. {
  10. // Stores return value
  11. int retVal = 0;
  12.  
  13. // This flag restricts the execution of the loop
  14. bool fFlag = true;
  15.  
  16. // While s1 and s2 exist
  17. while(*s1++ && *s2++ && fFlag)
  18. (retVal != 0) ? fFlag = false : (*s1 > *s2) ? ++retVal : (*s1 < *s2) ? --retVal : retVal = retVal;
  19. /* What the above statement does:
  20.  
  21.   Condition 1:
  22.  
  23.   retVal, if it has been modified to a value not equal to 0, then terminate the loop
  24.   by setting the flag to be false. This will restrict the value retVal to 0, 1, or -1.
  25.  
  26.   Condition 2:
  27.  
  28.   If the (Condition 1) fails, then it checks whether the current character pointed by
  29.   s1 is > the current character pointed by s2. If true, then increment value of retVal.
  30.  
  31.   Condition 3:
  32.   If (Condition 1) and (Condition 2) both fail, then decrement the value of retVal.
  33.   If this condition fails as well, then leave retVal alone (as *s1 == *s2).
  34.   */
  35.  
  36. // Return retVal, which can be 0, 1, or -1
  37. return retVal;
  38. }
  39.  
  40. /*
  41.   Function(Fn II) compares two strings until
  42.   (s1 or s2) terminates or uptil 'num' number of characters, &
  43.   returns (+1) if s1 > s2, (0) if s1 == s2,
  44.   and (-1) if s1 < s2
  45.  
  46.   Note: (all comparisns are ASCII based)
  47. */
  48. int aNew_strncmp(const char* s1, const char* s2, int num)
  49. {
  50. // Stores return value
  51. int retVal = 0;
  52.  
  53. // This flag restricts the execution of the loop
  54. bool fFlag = true;
  55.  
  56. while(*s1++ && *s2++ && --num > 0 && fFlag)
  57. (retVal != 0) ? fFlag = false : (*s1 > *s2) ? ++retVal: (*s1 < *s2) ? --retVal : retVal = retVal;
  58. // refer to the function above
  59.  
  60. // Return retVal, which can be 0, 1, or -1
  61. return retVal;
  62. }
tux4life May 28th, 2009 2:16 am
strcmp
can also be written like this :D:
int strcmp(const char *s1, const char *s2) {
    while( ( *s1 && *s2 ) && ( *s1++ == *s2++ ) );
    return *( --s1 ) - *( --s2 );
}
However, I didn't include code to avoid a NULL-pointer in my code, but it might be wise to do so as well :)

amrith92 May 28th, 2009 2:22 pm
Wow! Good code :) beats mine by a mile... :)


All times are GMT -4. The time now is 7:58 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC