在本教程中,我们将讨论仅更改一个字符即可将字符串转换为回文字符串的程序。
为此,我们将提供一个字符串。我们的任务是仅更改一个字符即可将给定的字符串转换为回文。
#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