char

字符串是否相等(大小写敏感)

两种方法:strcmp比较、转string再比较

#include <string.h>  //for C strcmp
#include <stdio.h>   //for C printf
#include <string>    //for C++ string

int main()
{
    char* cA = "Hello World";
    char cB[] = "Hello World";
    char cC[64] = "Hello World";
    if (strcmp(cA,cB)==0 && strcmp(cA,cC)==0 && strcmp(cA,"Hello World")==0)
    {
        printf("两个字符串相等\n");
    }
    else
    {
        printf("两个字符串不相等\n");
    }

    std::string sA = cA;    //char* 转 string
    if (sA=="Hello World")    
    {
        printf("两个字符串相等\n");
    }

    return 0;
}

 

posted @ 2024-01-22 19:51  夕西行  阅读(24)  评论(0编辑  收藏  举报