문자열이 회문인지를 결정하는 재귀 함수를 작성하려고합니다. 여기에 지금까지 무엇을 가지고 :재귀 함수로 문자열 Palindrome 찾기
int main()
{
string word = "madam";
if (palindrome(word) == true)
cout << "word is a palindrome!" << endl;
else
cout << "word is not a palindrome..." << endl;
return 0;
}
bool palindrome(string word)
{
int length = word.length();
string first = word.substr(0,1);
string last = word.substr((length - 1), 1);
if (first == last)
{
word = word.substr((0 + 1), (length - 2));
cout << word << " " << word.length() << endl; // DEBUGGING
if (word.length() <= 1) return true; // Problem line?
palindrome(word);
}
else
return false;
}
어떤 이유
재귀 기능이 충분히 깊은 가져오고 word.length()보다 작거나 1과 동일, 그건 사실 반환하지 않습니다. 나는 이유를 알 수없는 것 같습니다. 재귀 함수가 작동하는 방식과 관련이 있습니까? 또는 디버깅을 언급하기 전에 줄의 길이를 어떻게 다시 조정합니까?
저는 C++에서 재능이 없기 때문에 제 프로그래밍이 좋지 않으면 실례합니다.
'word.substr ((0 + 1)'1 – Maroun