回文:例如123321,12321这样的。
那么如何判断一个字符串是否是回文呢?
以下用两种方法解决这个问题。
第一种,用循环。代码如下:
bool IsPalindereme(char* str,int size)
{
int begin=0;//字符串第一个下标
int end=size-1;//字符串最后一个下标
while(begin<end)
{
if(str[begin]!=str[end])
return false;
begin++;
end--;
}
return true;
}
第二种呢,用递归实现。
bool IsPalindereme(char* str,int size)
{
//递归
if(size<=1)
return true;
if(str[0]!=str[size-1])
return false;
else
return IsPalindereme(++str,size-2);
}