曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5369|回复: 0
打印 上一主题 下一主题

[字符串] strcmp() --- 比较两个字符串

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2010-5-10 15:02:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
函数原型
#include <string.h>
int strcmp(const char *s1, const char *s2);
说明:
该函数比较字符串 s1 和 s2,如果 s1 等于 s2 则函数返回 0;如果 s1 包含 s2,则返回大于0的数;如果 s1 小于 s2,则返回小于0的数。
字符串的大小比较是以 ASCII 表上的顺序来决定的,这个顺序也是字符的值。

举例:
#include <stdio.h>
#include <string.h>

int main ()
{
        const char *p = "hello world";
        const char *p1 = "hello";
        const char *p2 = "hello world";
        const char *p3 = "hello world32";

        int k;

        k = strcmp (p, p2);
        printf ("两字符串相等,返回值为 %d\n", k);

        k = strcmp (p, p1);
        printf ("字符串1大于字符串2,返回值为 %d\n", k);

        k = strcmp (p, p3);
        printf ("字符串1小于字符串2,返回值为 %d\n", k);

        return (0);
}
运行与输出
./strcmp
两字符串相等,返回值为 0
字符串1大于字符串2,返回值为 1
字符串1小于字符串2,返回值为 -1
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 23:16 , Processed in 0.083346 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表