【发布时间】:2019-09-15 15:35:11
【问题描述】:
这是我的代码,但函数 countPalindromes 出于某种原因总是返回 0。我不明白为什么
bool isPalindrome(string s) {
int x=0;
int r= s.length() -1;
while(r>1){
if(s[x++]!=s[r--])
{
cout<<"Not Palindrome";
}
else
{
return true;
}
}
}
int countPalindromes(string s) {
int n= s.length();
int counter=0;
for(int i = 0; i<n ; i++)
{
for(int j=1; j< n -i ; j++)
{
if(isPalindrome(s.substr(i,j)=true))
{
counter+=1;
}
}
}
return counter;
}
int main() {
cout<<countPalindromes("ABA");
return 0;
}
【问题讨论】:
-
您需要学习如何使用调试器逐步执行代码,这将帮助您找到自己的错误。也就是说,作为这里的新用户,请使用tour 并阅读How to Ask。
-
这里有一堆错误。使用调试器单步执行逻辑并确保它在每个点上都按照您的意愿行事。
标签: c++ string substring palindrome