Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include <iostream>
#include <string.h>
using namespace std;
const char MAX_CHAR = 26;
void countFreq(char str[], int freq[], int n){
   for (int i = 0; i < n; i++)
      freq[str[i] - 'a']++;
bool canMakePalindrome(int freq[], int n){
   int count_odd = 0;
   for (int i = 0; i < 26; i++)
      if (freq[i] % 2 != 0)
      if (n % 2 == 0) {
         if (count_odd > 0)
            return false;
            return true;
      if (count_odd != 1)
         return false;
   return true;
bool isPalimdrome(char str[], int n){
   int freq[26] = { 0 };
   countFreq(str, freq, n);
   if (!canMakePalindrome(freq, n))
      return false;
   char odd_char;
   for (int i = 0; i < 26; i++) {
      if (freq[i] % 2 != 0) {
         odd_char = (char)(i + 'a');
   int front_index = 0, rear_index = n - 1;
   for (int i = 0; i < 26; i++) {
      if (freq[i] != 0) {
         char ch = (char)(i + 'a');
         for (int j = 1; j <= freq[i] / 2; j++) {
            str[front_index++] = ch;
            str[rear_index--] = ch;
   if (front_index == rear_index)
      str[front_index] = odd_char;
   return true;
void reverse(char str[], int i, int j){
   while (i < j) {
      swap(str[i], str[j]);
bool nextPalindrome(char str[], int n){
   if (n <= 3)
      return false;
   int mid = n / 2 - 1;
   int i, j;
   for (i = mid - 1; i >= 0; i--)
      if (str[i] < str[i + 1])
      if (i < 0)
         return false;
   int smallest = i + 1;
   for (j = i + 2; j <= mid; j++)
      if (str[j] > str[i] && str[j] < str[smallest])
         smallest = j;
   swap(str[i], str[smallest]);
   swap(str[n - i - 1], str[n - smallest - 1]);
   reverse(str, i + 1, mid);
   if (n % 2 == 0)
      reverse(str, mid + 1, n - i - 2);
      reverse(str, mid + 2, n - i - 2);
   return true;
void printAllPalindromes(char str[], int n){
   if (!(isPalimdrome(str, n))) {
   do {
   } while (nextPalindrome(str, n));
int main(){
   char str[] = "abccba";
   int n = strlen(str);
   cout<<"The list of palindromes possible is :\n";
   printAllPalindromes(str, n);
   return 0;

What I have tried:

i want the above code to convert in python
Updated 4 days ago
KarstenK 5-Jul-20 3:21am
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is not a code translation service: we are not here to do that for you. Even if we were, code translation does not produce "good code " in the target language. And that is barely C++ - it's C with cout calls instead of printf!

Either learn Python, learn C++ and use the C++ code as a specification for your Python app, or find a code fragment in Python you can hand in as your homework.

Or ... and this is just a wild idea ... you could write your own code to solve the problem?
Rate this:
Please Sign up or sign in to vote.

Solution 5

Convert C++ code to Python

Bad idea!
Because the way to handle things is different from 1 language to another.
It is like trying to translate sayings between languages bacause it is linked to culture and history.
If a French tells you: "Brancher le fil rouge sur le bouton rouge, et le fil vert sur le bouton vert".
it translate to "connect the red wire to the red button, and the green wire to the green button".
For anyone speaking English, it just means what the words say, there is no special meaning.
For French people, it refer to something obvious which turn non obvious because it refer to a french movie with the sentence, and buttons were blue and white.[^]

So the only efficient way to translate code means that you need to know both languages in deep. It is so true that most programmers find it easier to extract the algorithm from original code and write from scratch in the second language.
Stefan_Lang 4 days ago
"and buttons were blue and white"
That explains a lot about C++ ;-)

That said, you fell for a necroed posting (from july). Not sure if your advice reaches the author (in time).
Patrice T 3 days ago
I know, I seen it after posting :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100