2012-06-24 132 views
-2

如何找到一個字符串是否存在於另一個字符串中,而不是使用字符串比較函數,而是遍歷每個字符並在C++中測試相等性?找到一個字符串,如果它存在於另一個字符串

string one="hello world"; // Search *in* this string 
string two="wor";   // Search *for* this string 
+0

請嘗試編輯您的問題,所以這將是更清晰..現在很難理解你的要求.. –

+1

如果你「嗎必要的「並編輯你的問題,你可能會得到更多的答覆。 – mathematician1975

+0

我同意@NicoMayer - 這看起來像作業。如果是這樣,請務必通過添加[tag:homework]標籤來表明這一點。此外,您應該嘗試解決問題並顯示您所取得的進展。我們很樂意爲您提供幫助,但我們不想只爲您做。 –

回答

0

看起來像功課^^

int find(string one, string two){ 
    int a, b; 
    for(int c = 0; c + two.length() < one.length(); c++){ 
     a = 0; 
     b = c; 
     while(a < two.length() && one[b++] == two[a++]); 
     if(a == two.length())return c; 
    } 
    return -1; 
} 
+0

如果您認爲這是作業,您應該避免發佈完整的答案。 –

相關問題