您的位置:首页 > 编程语言 > C语言/C++

【编程题】-C++实现:判断字符串在末尾加一个字符,能否构成回文串

2015-09-19 23:47 495 查看
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  @题目:判断字符串是否回文
//  此题:判断字符串在末尾加一个字符,能否构成回文串。

//  @时间 ; 2015.09.19
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 思路:判断arr[1]~arr
是否回文即可
#include <iostream>
#include <string.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

//  函数声明
bool IsPalindromeStrCore(char* pStr);
bool IsPalindromeStr(char* pStr);

int main(int argc, char** argv)
{
char szBuf[] = "coco";
if (IsPalindromeStr(szBuf))
{
printf("YES");
}
else
{
printf("NO");
}

return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  函数实现
bool IsPalindromeStrCore(char* pStr)
{
int len = strlen(pStr);

for (int i = 0 , j = len - 1 ; i < j ; ++i , --j)
{
if (pStr[i] != pStr[j])
return false;
}

return true;
}

bool IsPalindromeStr(char* pStr)
{
if (NULL == pStr || strlen(pStr) > 10 || strlen(pStr) < 2)
{
return false;
}
else
{
return IsPalindromeStrCore(pStr + 1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: