C语言strcmp函数是什么样的代码

C语言strcmp函数是什么样的代码

C语言中strcmp函数是string库的常用函数。其原型代码和介绍如下:

1.先说一下这个函数的实现原理,向strcmp()函数中传入两个字符串(记为str1,str2).传入之后,通过把str1的各字母的ASCII码值和str2的各字母的ASCII码值进行比较。若str1>str2则返回正数,若str1=str2则返回0,否则,则返回负数。

2.下面实现代码:

3.整个函数的原型中得益于“(*str1!='\0')&&(*str1==*str2)”这一句的代码,因为这样当字符指针指向空,意味着字符串已经移动到最后了,比较结束,此时可以退出循环。而如果两个字符串不相等时,则此时函数也可以退出了。

扩展资料:

这个函数要注意一下几点:

①使用*(unsigned char*)str1而不是用*str1。这是因为传入的参数为有符号数,有符号字符值的范围是-128~127,无符号字符值的范围是0~255,而字符串的ASCII没有负值,若不转化为无符号数这回在减法实现时出现错误。

②While循环中ret=*(unsigned char*)str1-*(unsigned char*)str2) && *str1,最后与上str1也可以换成str2,因为前面已经做了相减,无论哪个先为‘\0’都会退出。因为最后与上str1是为了判断str1是否结束,即是否为‘\0’。

③这个函数没有判断参数为NULL时的情况,所以当传入NULL时程序会崩溃。网上看别人说商业化代码都会在调用strcmp前先判断是否为NULL,所以可以不用判断NULL;我在VC6上测试string.h中的strcmp(NULL,NULL),程序也会崩溃。这里可以根据实际情况来决定。

参考资料来源:百度百科-strcmp函数

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-21
strcmp(s1,s2)
相同返回0
当s1<s2时,返回值<0 当s1=s2时,返回值=0 当s1>s2时,返回值>0 即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止。如: "A"<"B" "a">"A" "computer">"compare" 特别注意:strcmp(s1,s2)这里面只能比较字符串,不能比较数字等其他形式的参数。
第2个回答  2010-12-21
strcmp简介:
函数原型int strcmp(const char *str1,const char *str2);
其作用为
将str1的各个字母的ASCII码与str2的进行比较。
若str1>str2则返回整数,若str1=str2返回0,否则,返回负数
一般这样调用:
if(strcmp(str1,str2)==0)...
else ...
当然,这是C语言的库函数,它的代码存储在某个库文件中。
在string.h里只是一个声明。
我给你写一下,大概是这样的:
int strcmp(const char *str1,const char *str2)
{
char *str3=str1,*str4=str2;
while(*str3++!=*str4++&&(*str3||*str4))
{
if(*--str3<*--str4)
return -1;
if(*str3>*str4) return 1;
str3++;str4++;
}
return 0;
}本回答被提问者采纳
第3个回答  2020-02-08
第4个回答  2010-12-21
page ,132
title strcmp.asm - compare two strings
;***
;strcmp.asm - routine to compare two strings (for equal, less, or greater)
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
; STRCMP compares two strings and returns an integer
; to indicate whether the first is less than the second, the two are
; equal, or whether the first is greater than the second, respectively.
; Comparison is done byte by byte on an UNSIGNED basis, which is to
; say that Null (0) is less than any other character (1-255).
;
;*******************************************************************************

.xlist
include cruntime.inc
.list

page
;***
;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:
;
;*******************************************************************************

CODESEG

public strcmp
strcmp proc \
str1:ptr byte, \
str2:ptr byte

OPTION PROLOGUE:NONE, EPILOGUE:NONE

.FPO ( 0, 2, 0, 0, 0, 0 )

mov edx,[esp + 4] ; edx = src
mov ecx,[esp + 8] ; ecx = dst

test edx,3
jnz short dopartial

align 4
dodwords:
mov eax,[edx]

cmp al,[ecx]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 1]
jne short donene
or ah,ah
jz short doneeq

shr eax,16

cmp al,[ecx + 2]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 3]
jne short donene
add ecx,4
add edx,4
or ah,ah
jnz short dodwords

align 4
doneeq:
xor eax,eax
ret

align 4
donene:
; The instructions below should place -1 in eax if src < dst,
; and 1 in eax if src > dst.

sbb eax,eax
sal eax,1
add eax,1
ret

align 4
dopartial:
test edx,1
jz short doword

mov al,[edx]
add edx,1
cmp al,[ecx]
jne short donene
add ecx,1
or al,al
jz short doneeq

test edx,2
jz short dodwords

align 4
doword:
mov ax,[edx]
add edx,2
cmp al,[ecx]
jne short donene
or al,al
jz short doneeq
cmp ah,[ecx + 1]
jne short donene
or ah,ah
jz short doneeq
add ecx,2
jmp short dodwords

strcmp endp

end