关于库函数STRCMP的实现

Double_Lan_2975 2010-04-08 12:59:58
我在C语言现代方法这本书里面看到了STRCMP是这样实现的,觉得有点问题,想提出来高手们讨论下
int strcmp(char *s,char *t)
{

int i;
for(i=0;s[i]==t[i];i++)
if(s[i]=='\0')
return 0;
return s[i]-t[i];
}

假设有 这两个字符串ABC ABCD 貌似这个算法不行啊
我看这么改行不:
int strcmp(char *s,char *t)
{

int i;
for(i=0;s[i]==t[i];i++)
if(s[i]=='\0'&&t[i]=='\0')
return 0;
return s[i]-t[i];
}
...全文
806 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
JeffShen 2011-04-15
  • 打赏
  • 举报
回复
楼主,在你所列的程序中可能少写了一组“{}”
int strcmp(char *s ,char *t)
{
int i=0;
for(i=0;s[i]==t[i];i++)
{
if(s[i]=='\0')
return 0;
}
return s[i]-t[i];
}
mymtom 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ypb362148418 的回复:]

C/C++ code

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

while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) &&am……
[/Quote]
不觉得呀。
ypb362148418 2010-04-08
  • 打赏
  • 举报
回复

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

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

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

return( ret );
}
//这个代码才是王道
huanmie_09 2010-04-08
  • 打赏
  • 举报
回复
问下楼主,你觉得这个程序怎么有问题了。我倒是觉得楼主添加的那个判断有点画蛇添足了.
注意for循环里的条件表达式为:s[i]==t[i];
if(s[i]=='\0') {
return 0;
}
是判断等长且各字符相等的两个字符串的时候.
其他不等的情况均可以由s[i]==t[i]来判断,若不成立,跳出for循环.
返回值为不相等的两个字符的差。

int strcmp(char *s,char *t)
{
int i;
for(i=0;s[i]==t[i];i++) {
if(s[i]=='\0') {
return 0;
}
}
return s[i]-t[i];
}
mymtom 2010-04-08
  • 打赏
  • 举报
回复

#include <string.h>

/*
* Compare strings.
*/
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}
muyiyj 2010-04-08
  • 打赏
  • 举报
回复
我觉得没必要添加,
你for中已经做了s[i]==t[i];判断
你if中就没必要添加&&t[i]=='\0'了
mstlq 2010-04-08
  • 打赏
  • 举报
回复
glibc的……
/* Copyright (C) 1991, 1996, 1997, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */

#include <string.h>
#include <memcopy.h>

#undef strcmp

/* Compare S1 and S2, returning less than, equal to or
greater than zero if S1 is lexicographically less than,
equal to or greater than S2. */
int
strcmp (p1, p2)
const char *p1;
const char *p2;
{
register const unsigned char *s1 = (const unsigned char *) p1;
register const unsigned char *s2 = (const unsigned char *) p2;
unsigned reg_char c1, c2;

do
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (c1 == c2);

return c1 - c2;
}
libc_hidden_builtin_def (strcmp)
jackyjkchen 2010-04-08
  • 打赏
  • 举报
回复
微软CRT的实现,不要去鸟那些教材,VC才是用于生产实践的


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

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

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

return( ret );
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧