Minimum number of Appends needed to make a string palindrome in C++

Problem statement

Given a string, find minimum characters to be appended to make a string palindrome.


If string is abcac then we can make string palindrome by appending 2 highlighed characters i.e. abcacba


  • Check if string is already palindrome, if yes then no need to append any characters.
  • One by one remove a character from string and check whether remaining string is palindrome or not
  • Repeat above process until string becomes palidrome
  • Return the number of characters removed so far as a final answer


#include <iostream>
#include <cstring>
using namespace std;
bool isPalindrome(char *str) {
   int n = strlen(str);
   if (n == 1) {
      return true;
   int start = 0, end = n - 1;
   while (start < end) {
      if (str[start] != str[end]) {
         return false;
   return true;
int requiredAppends(char *str) {
   if (isPalindrome(str)) {
      return 0;
   return 1 + requiredAppends(str + 1);
int main() {
   char *str = "abcac";
   cout << "Characters to be appended = " << requiredAppends(str) << endl;
   return 0;


When you compile and execute above program. It generates following output −

Characters to be appended = 2

Updated on: 22-Nov-2019


Kickstart Your Career

Get certified by completing the course

Get Started