Convert the string into palindrome string by changing only one character in C++


In this tutorial, we will be discussing a program to convert the string into palindrome string by changing only one character.

For this we will be provided with a string. Our task is to convert the given string into a palindrome by changing only one character.

Example

 Live Demo

#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;
}

Output

Yes

Updated on: 22-Jan-2020

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements