// HuiWenChuan.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
/*
递归判断是否是回文串
回文串:"abccba", "aba"
*/
bool isHuiWenChuan(const string &strTest)
{
static int i = 0;
int j = strlen(strTest.c_str())-1-i;
if (strTest[i] == strTest[j])
{
if (j-i <= 2)
{
return true;
}
else
{
i++;
return isHuiWenChuan(strTest);
}
}
else
{
return false;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string strVal = "abccba";
string strVal2 = "abcbba";
string strVal3 = "tlt";
bool bH = isHuiWenChuan(strVal);
bool bH2 = isHuiWenChuan(strVal2);
bool bH3 = isHuiWenChuan(strVal3);
return 0;
}
判断回文串的递归函数实现(c++)
最新推荐文章于 2024-02-21 12:34:05 发布
本文介绍了一种使用C++实现的递归方法来判断一个字符串是否为回文串。通过比较字符串首尾字符的方式逐步向中间逼近,直至完成整个字符串的检查。
1122

被折叠的 条评论
为什么被折叠?



