c - 如何比较C中指向字符串的指针

标签 c

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

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

最佳答案

您可能想使用 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;
}

结果:

'hello' is less than 'world'.

关于c - 如何比较C中指向字符串的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3663668/

相关文章:

c++ - 多边形和方形纹理的纹理坐标

c - 如何评估语句*ptr1++ = *ptr2++?

使用POW计算不同数字类型的范围

objective-c - 在 obj-c 对象而不是 C 结构上调用 IOUSBDeviceInterface 函数

c - sizeof(<字符串数组>) 始终返回 8

c - C 将如何解析 a = b---c?

c - While 循环不适用于 C 中的 bool 语句(初学者!)

android - 使用 ARM neon 将短数组转换为 float

从 BYTE 到 DWORD 的转换

c - 减少静态库大小的编译器选项和其他机制是什么?