通过仅更改 C++ 中的一个字符将该字符串转换为回文字符串
在本教程中,我们将讨论一个程序,该程序可以通过更改仅一个字符将该字符串转换为回文字符串。
为此,我们将获得一个字符串。我们的任务是通过更改仅一个字符将给定的字符串转换为回文。
示例
#include<bits/stdc++.h>
using namespace std;
//checking if conversion to palindrome
//is possible
bool if_palindrome(string str){
int n = str.length();
//counting number of characters
//to be changed
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP