如何将指针与C中的字符串进行比较

Rn2*_*2dy 7 c

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

char *str1 = "hello";
char *str2 = "world";
//compare str1 and str2 ?
Run Code Online (Sandbox Code Playgroud)

Dan*_*llo 9

您可能想要使用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;
}
Run Code Online (Sandbox Code Playgroud)

结果:

'hello' is less than 'world'.
Run Code Online (Sandbox Code Playgroud)


And*_*rsK 5

if ( strcmp( str1, str2 ) == 0 )
  same
Run Code Online (Sandbox Code Playgroud)