

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Check for Palindrome after every character replacement Query in C++
Consider we have a string and some queries in set Q. Each query contains a pair of integers i and j. and another character c. We have to replace characters at index i and j with the new character c. And tell if the string is palindrome or not. Suppose a string is like “AXCDCMP”, if we use one query like (1, 5, B), then the string will be “ABCDCBP”, then another query like (0, 6, A), then it will be “ABCDCBA”, this is palindrome.
We have to create one query using indices i, j, then replace the characters present at indices i, j in the string, with c.
Example
#include <iostream> using namespace std; class Query{ public: int i, j; char c; Query(int i, int j, char c){ this->i = i; this->j = j; this->c = c; } }; bool isPalindrome(string str){ int n = str.length(); for (int i = 0; i < n/2 ; i++) if (str[i] != str[n-1-i]) return false; return true; } bool palindromeAfterQuerying(string str, Query q[], int n){ for(int i = 0; i<n; i++){ str[q[i].i] = q[i].c; str[q[i].j] = q[i].c; if(isPalindrome(str)){ cout << str << " is Palindrome"<< endl; }else{ cout << str << " is not Palindrome"<< endl; } } } int main() { Query q[] = {{1, 5, 'B'}, {0, 6, 'A'}}; int n = 2; string str = "AXCDCMP"; palindromeAfterQuerying(str, q, n); }
Output
ABCDCBP is not Palindrome ABCDCBA is Palindrome
- Related Questions & Answers
- Apostrophe replacement in MySQL query?
- Longest Repeating Character Replacement in C++
- 8085 Program to check for palindrome
- How to check for palindrome in R?
- Python – Insert character in each duplicate string after every K elements
- Program to check for palindrome in 8085 Microprocessor
- JavaScript example for Capturing mouse positions after every given interval
- C++ program to find the quantity after mixture replacement
- C++ program to find string after adding character 'a' string becomes non-palindrome
- Check Palindrome in Java Program
- Create palindrome by changing each character to neighboring character in JavaScript
- JavaScript - Find if string is a palindrome (Check for punctuation)
- How to check a String for palindrome using arrays in java?
- C++ Program for Optimal Page Replacement Algorithm
- Palindrome in Python: How to check a number is palindrome?
Advertisements