社区
C语言
帖子详情
字符串如何用大于小于比较大小?
vn68214
2006-12-17 04:50:06
一个一个字符判断太麻烦了,有没有更好的算法亚
...全文
884
15
打赏
收藏
字符串如何用大于小于比较大小?
一个一个字符判断太麻烦了,有没有更好的算法亚
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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();
肯定要一个一个的比。
字符串
如何
比较大小
字符比较(character comparison)是指按照字典次序对单个字符或
字符串
进行
比较大小
的操作,一般都是以ASCII码值的大小作为字符比较的标准。 【比较方式】 可以使用String类的compareTo()方法来实现。该方法用于判断一个
字符串
是
大于
、等于还是
小于
另一个
字符串
,返回int类型的差值。判断
字符串
大小的依据是它们在字典中的顺序。 实现Comparable接口 实现Comparator接口 String s1 = "abc"; String s2 = "efg"; System.o.
【Python】
字符串
是如何
比较大小
的?
引言 Python 中的
字符串
其实也是可以
比较大小
的,如下: >>> 'a' < 'b' True >>> 'aa' < 'ab' True >>> 'abc' < 'acb' True 本文简单介绍一下 Python 中
字符串
比较大小
的原理。 原理 查阅 Python 官网文档可知,
字符串
是通过计算每个字符的 Unicode 编码来
比较大小
的。其中,计算 Unicode 编码使用 Python 内置的 ord() 函数。 Stri
java怎么比较abcd大小,java中两个
字符串
长度相等怎么
比较大小
?
满意答案kpnw82142016.12.24采纳率:59%等级:8已帮助:561人使用 String.compareTo 方法:compareTo() 的返回值是int, 它是先比较对应字符的大小(ASCII码顺序)1、如果
字符串
相等返回值02、如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值)(负值前
字符串
的值
小于
后
字符串
,正值前
字符串
大于
后
字符串
)3、...
【C语言】正确比较两个
字符串
目录 两种常见的错误比较方法 (1)为什么两个
字符串
不能直接用
大于
小于
号比较? (2)为什么不能用数组名直接比较
字符串
比较大小
的实质 两种比较
字符串
两种方法 (1)难的方法 (2)简单的方法 两种常见的错误比较方法 你还在用‘>’‘<’‘=’等比较
字符串
吗? 事实上,用
大于
小于
比较
字符串
的方法是不对的。我们看一下两种常见的错误方法 #include<stdio.h> int main() { char arr1[] = "abcdef"; char
Java
字符串
比较大小
总结 compareTo() 按字典顺序比较两个
字符串
compareTo()返回值: int 返回值类型分别有三种,
小于
0,等于0,
大于
0 如果
字符串
相等返回值0; 如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值)(负值前
字符串
的值
小于
后
字符串
,正值前
字符串
大于
后
字符串
); 如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度。 简介 按字典顺序比较两个
字符串
。
C语言
70,036
社区成员
243,244
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章