如何比较指针在C字符串 [英] How to compare pointer to strings in C

查看:108
本文介绍了如何比较指针在C字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较C两个字符串?帮帮我吧,我是初学者@@

 的char * STR1 =你好;
的char * str2的=世界;
//比较str1和str2的?


解决方案

您可能需要使用的 STRCMP

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,字符** argv的)
{
    INT伏;
    为const char * STR1 =你好;
    为const char * str2的=世界;    V = STRCMP(STR1,STR2);    如果(ⅴ℃,)
        的printf(%s的是小于'%s'的\\ n,STR1,STR2);
    否则如果(ⅴ== 0)
        的printf(%s'的等于'%s'的\\ n,STR1,STR2);
    否则如果(V&0)
        的printf(%s'的比'%s'的更大\\ n,STR1,STR2);    返回0;
}

结果:

 '你好'小于'世界'。

how to compare two strings in C? Help me, I am beginner@@

char *str1 = "hello";
char *str2 = "world";
//compare str1 and str2 ?

解决方案

You may want to use strcmp:

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

int main(int argc, char **argv)
{
    int v;
    const char *str1 = "hello";
    const char *str2 = "world";

    v = strcmp(str1, str2);

    if (v < 0)
        printf("'%s' is less than '%s'.\n", str1, str2);
    else if (v == 0)
        printf("'%s' equals '%s'.\n", str1, str2);
    else if (v > 0)
        printf("'%s' is greater than '%s'.\n", str1, str2);

    return 0;
}

Result:

'hello' is less than 'world'.

这篇关于如何比较指针在C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆