strcmp实现

;***
;strcmp - compare two strings, returning less than, equal to, or greater than
;
;Purpose:
;       Compares two string, determining their lexical order.  Unsigned
;       comparison is used.
;
;       Algorithm:
;          int strcmp ( src , dst )
;                  unsigned char *src;
;                  unsigned char *dst;
;          {
;                  int ret = 0 ;
;
;                  while( ! (ret = *src - *dst) && *dst)
;                          ++src, ++dst;
;
;                  if ( ret < 0 )
;                          ret = -1 ;
;                  else if ( ret > 0 )
;                          ret = 1 ;
;
;                  return( ret );
;          }
;
;Entry:
;       const char * src - string for left-hand side of comparison
;       const char * dst - string for right-hand side of comparison
;
;Exit:
;       AX < 0, 0, or >0, indicating whether the first string is
;       Less than, Equal to, or Greater than the second string.
;
;Uses:
;       CX, DX
;
;Exceptions:
;
;*******************************************************************************

 
 
 
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

char *mystrcmp(char *src, const char *dst)
{
	int ret = 0;

	while (!(ret = *src - *dst) && *dst)
		++src, ++dst;

	if (ret < 0)
		ret = -1;
	else if (ret > 0)
		ret = 1;

	return (ret);
}

int main(int argc, char* argv[])
{
	char dst[] = "dest";
	char *src = "desr";

	printf("%d", mystrcmp(dst, src));
	return 0;
}
posted @ 2012-12-05 20:01  helloweworld  阅读(189)  评论(0编辑  收藏  举报