如何比较 char 变量(c 字符串)?

2022-01-25 00:00:00 if-statement compare c++
#include <iostream>
using namespace std;

int main() {
    char word[10]="php";
    char word1[10]="php";

    if(word==word1){
        cout<<"word = word1"<<endl;
    }

return 0;
}

我不知道如何比较两个 char 字符串来检查它们是否相等.我当前的代码不起作用.

I don't know how to compare two char strings to check they are equal. My current code is not working.

推荐答案

使用 std::string 对象代替:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string word="php";
    string word1="php";

    if(word==word1){
        cout<<"word = word1"<<endl;
    }

return 0;
}

相关文章