通过仅更改C ++中的一个字符即可将字符串转换为回文字符串
在本教程中,我们将讨论仅更改一个字符即可将字符串转换为回文字符串的程序。
为此,我们将提供一个字符串。我们的任务是仅更改一个字符即可将给定的字符串转换为回文。
示例
#include<bits/stdc++.h>
using namespace std;
//检查是否转换为回文
//有可能
bool if_palindrome(string str){
int n = str.length();
//计算字符数
//待更改
int count = 0;
for (int i = 0; i < n/2; ++i)
if (str[i] != str[n - i - 1])
++count;
return (count <= 1);
}
int main(){
string str = "abccaa";
if (if_palindrome(str))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}输出结果
Yes
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短