字符串如何用大于小于比较大小?

vn68214 2006-12-17 04:50:06
一个一个字符判断太麻烦了,有没有更好的算法亚
...全文
863 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
gangjh 2006-12-18
  • 打赏
  • 举报
回复
字符不長可以這樣比較
char *str="hello" ;
if ((int *) str == (int *) "hello") {
printf("eq\n") ;
}
flyforlove 2006-12-18
  • 打赏
  • 举报
回复
最后怎么也要一个一个比。
柯本 2006-12-18
  • 打赏
  • 举报
回复
早期的strcmp源程序(来源:1985 Software Development Systems, Inc.)
int
strcmp(s1,s2)
register const char *s1;
register const char *s2;
{
while( (*s1 == *s2) && (*s1) ) ++s1, ++s2;

return ((int)(unsigned char)*s1) - ((int)(unsigned char)*s2);
}

另一个small C 的:
/*
** return <0, 0, >0 aUording to
** s<t, s=t, s>t
*/
strcmp(s, t) char *s, *t; {
while(*s == *t) {
if(*s == 0) return (0);
++s; ++t;
}
return (*s - *t);
}
现在在intel平台,用asm写的:(来源:Borland )
Code_seg@
Func@ strcmp, _EXPFUNC, _RTLENTRYF, <pointer str1>, <pointer str2>
mov ecx, [esp+4] ; str1
mov edx, [esp+8] ; str2
push ebx
xor eax, eax
xor ebx, ebx

PSloop:
mov al, [ecx]
mov bl, [edx]
sub eax, ebx
jne PSend
test bl,bl
je PSend

mov al, [ecx+1]
mov bl, [edx+1]
sub eax, ebx
jne PSend
test bl,bl
je PSend

mov al, [ecx+2]
mov bl, [edx+2]
sub eax, ebx
jne PSend
test bl,bl
je PSend

mov al, [ecx+3]
mov bl, [edx+3]
sub eax, ebx
jne PSend
add ecx, 4
add edx, 4
test bl, bl
jne PSloop

PSend:
pop ebx
Return@

EndFunc@ strcmp

Code_EndS@

end

sirguan 2006-12-18
  • 打赏
  • 举报
回复
可以用memcpy,库作了优化。不过还需要自己多做点处理。
greenteanet 2006-12-17
  • 打赏
  • 举报
回复
本来字符串就是这样子比较的吧。
WaterWalker 2006-12-17
  • 打赏
  • 举报
回复
char p[] = "HTTP";

if(*(unsigned long *)p != *(unsigned long *)"HTTP")
{
printf("error!\n");
}
else
{
printf("right!\n");
}



http://bbs.chinaunix.net/viewthread.php?tid=869860&extra=page%3D3
lockhall 2006-12-17
  • 打赏
  • 举报
回复
一个个比较怎么就麻烦了?

很明了啊~
cmail 2006-12-17
  • 打赏
  • 举报
回复
自己写。
vn68214 2006-12-17
  • 打赏
  • 举报
回复
如果不准用string。h呢,只能自己写?
jixingzhong 2006-12-17
  • 打赏
  • 举报
回复
还有 类似的函数:

函数名: strncmp
功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:

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

int main(void)

{
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
int ptr;

ptr = strncmp(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");

ptr = strncmp(buf2,buf3,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");

return(0);
}
jixingzhong 2006-12-17
  • 打赏
  • 举报
回复
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
程序例:

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

int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;

ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");

ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");

return 0;
}
jixingzhong 2006-12-17
  • 打赏
  • 举报
回复
int strcmp(const char *s1,const char *s2)
比较字符串s1与s2的大小,并返回s1-s2
vn68214 2006-12-17
  • 打赏
  • 举报
回复
明白,不过这就是一个一个比啊
yeknight 2006-12-17
  • 打赏
  • 举报
回复
strcmp(*p,*q);
cmail 2006-12-17
  • 打赏
  • 举报
回复
库函数strcmp();

肯定要一个一个的比。

70,023

社区成员

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

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