
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Palindrome by Changing 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
#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
Advertisements