를 사용하여 다른 문자열을 찾을 수 :검색 문자열이 나는 다음과 같은 간단한 프로그램에서 일하고 있어요 재귀
/**
Recursively searches a string to find a second string.
@param s the string to search through.
@param t the string to search for
@return true if t is found in s.
Tests whether the string t is contained in a string s.
For instance, calling the function like this:
bool b = find("Mississippi", "sip");
returns true, since "sip" is contained in "Mississippi".
You must write this as a recursive function, not by just
calling the string::find() function, or by using a loop.
*/
bool find(const string& s, const string& t)
{
string temp = s;
if(temp.size() < t.size())
return false;
temp.erase(0, 1);
find(temp, t);
}
가 여기 내 테스트 출력입니다 : 나는 그것에 대해 4 다른 방법을 서면으로 작성했습니다
Checking function: Checking the find recursive function. -------------
+ find("Mississipi", "ipi")
X find("Mississipi", "ipx") should be false, but is true.
+ find("Sommertown", "Som")
+ find("Sommertown", "Sommertowne")
+ find("Somewhere in the middle", "in")
----------------------------------------------------------------------
Tests passing 4/5 (80%).
, 모두 비슷한 ... temp.erase 대신 find (temp.substr (1), t)를 포함하는 것을 포함합니다.
아무도 올바른 방향으로 나를 가리키지 않을까요? 나는 그것이 단순한 실수라는 것을 알고 있지만 나는 그것을 보지 않고있다!
감사합니다.
분명히, 당신의 함수에 오직 하나의 리턴이 있기 때문에 그것은 단지'false'를 리턴 할 것입니다 ... – Johan
당신의 의견은 당신의 프로그램보다 길다. 간단하게 유지 :) – keyser
'find'의 모든 경로가 값을 반환하는 것은 아닙니다. 또한 '찾기'는 실제로 '임시'또는 's'와 't'를 비교하지 않으며 그 반대도 마찬가지이므로 's'에서 실제로 't'를 찾지 않습니다. –