0

我正在编写一个程序来检测网络钓鱼。我正在尝试检查 URL 的基础是否在标签中相同。例如在 http://maps.google.com"> www.maps.yahoo.com 我试图检查 URL 的最后 2 部分是否相同,即 google.com = yahoo.com 或不是。

我正在使用以下代码来执行此操作:

void checkBase(char *add1, char *add2){
    char *base1[100], *base2[100];
    int count1 = 0, count2 = 0;
    base1[count1] = strtok(add1, ".");
        while(base1[count1] != NULL){
         count1++;
         base1[count1] = strtok(NULL, ".");
    }
    base2[count2] = strtok(add2, ".");
    while(base2[count2] != NULL){
    count2++;
    base2[count2] = strtok(NULL, ".");
    }
    if((base1[count1-1] != base2[count2-1]) && (base1[count1-2] != base2[count2-2])){
         cout << "Bases do not match: " << endl
          << base1[count1-2] << "." << base1[count1-1] << " and "
          << base2[count2-2] << "." << base2[count2-1] << endl;
    }
    else{
        cout << "Bases match: " << endl
              << base1[count1-2] << "." << base1[count1-1] << " and "
                  << base2[count2-2] << "." << base2[count2-1] << endl;

    }
 }

我不确定我在 if 语句中的比较是否正确。我正在传递两个 URL。谢谢

4

2 回答 2

0

这是比较两个指针char*(正如你指出的那样;))

base1[count1-1] != base2[count2-1])

改用这个

strcmp(base1[count1-1], base2[count2-1]) != 0

你可以使用std:stringboost tokenizer(我认为现在是 C++11)

问候

于 2013-05-27T21:50:17.127 回答
0

您不能通过比较它们的地址来比较字符串,两个相同的字符串可以存储在不同的地址中。要比较它们,您应该使用 strcmp:

 if(strcmp(base1[count1-1], base2[count2-1]) != 0 || 
    strcmp(base1[count1-2], base2[count2-2])!=0){
        std::cout << "Bases do not match: " << std::endl
            << base1[count1-2] << "." << base1[count1-1] << " and "
            << base2[count2-2] << "." << base2[count2-1] << std::endl;
    }

您可以使用 C++ 工具进行类似操作:

void checkBase(std::string a1, std::string a2){
    size_t a1_start = a1.rfind('.'), a2_start = a2.rfind('.');
    a1_start = a1.rfind('.', a1_start-1);
    a2_start = a2.rfind('.', a2_start-1);
    std::string h1 = a1.substr(a1_start+1), h2 = a2.substr(a2_start+1);
    if (h1 == h2)
        std::cout << "same" << std::endl;
    else
        std::cout << "not same" << std::endl;
}
于 2013-05-27T21:50:24.793 回答