string c ++ ==比较

时间:2016-03-26 22:46:59

标签: c++ arrays char

    char words[5] = { 't', 'd' , 'o', 'g', 'i', };
    cout << "enter letter ";


    char dogsearch[3] = { 'd', 'o', 'g' };

    if (words == dogsearch)
        cout << "found words";
    else
        cout << "Not found word";

我有两个数组,并相互比较,以找到相似性,并在彼此之间的差异,如果有类似的我想输出它是如何,反之亦然

1 个答案:

答案 0 :(得分:0)

您可以使用string :: find查找子字符串匹配

char words[5] = { 't', 'd' , 'o', 'g', 'i', };
char dogsearch[3] = { 'd', 'o', 'g' };

string words_str(words, sizeof(words));
string needle(dogsearch, sizeof(dogsearch));

size_t pos = words_str.find(needle);
if (pos != std::string::npos)
   cout << "found words at position " << pos << '\n';
else
   cout << "Not found word;