2013-05-27 79 views
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語句是正確的比較或不是我傳遞兩個網址。 感謝

+0

不是本有庫? – chris

+0

可能重複[什麼是比較兩個C風格字符串的正確功能?](http://stackoverflow.com/questions/9122000/what-is-the-proper-function-for-comparing-two-c-風格字符串) – jogojapan

回答

0

這是比較兩個指針的char *(正如你指出;))

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

使用這個代替

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

你可以使用STD:字符串升壓標記生成器(現在的C++ 11我認爲)

問候

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; 
}