Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert this C++ code to C to print all possible palindromes of a string #includ

ID: 3592182 • Letter: C

Question

Convert this C++ code to C to print all possible palindromes of a string


#include <bits/stdc++.h>
#include <iostream>
#include<vector>
using namespace std;
bool isPalindrome(string strData, int first, int last)
{
while (first < last)
{
if (strData[first] != strData[last])
return false;
first++;
last--;
}
return true;
}



void allPalPartUtility(vector<vector<string> >&allPart, vector<string> &currentPart, int startPos, int endPos, string strData)
{
if (startPos >= endPos)
{
allPart.push_back(currentPart);
return;
}
for (int co = startPos; co < endPos; co++)
{
if (isPalindrome(strData, startPos, co))
{
currentPart.push_back(strData.substr(startPos, co - startPos + 1));
allPalPartUtility(allPart, currentPart, co + 1, endPos, strData);
currentPart.pop_back();
}
}
}



void allPalPartitions(string strData)
{
int no = strData.length();
int ro, co;
vector<vector<string> > allPart;
vector<string> currentPart;
allPalPartUtility(allPart, currentPart, 0, no, strData);
cout<<" Palindromic decomposition of string: ";
for (ro = 0; ro < allPart.size(); ro++ )
{
for (co = 0; co < allPart[ro].size(); co++)
cout << allPart[ro][co] << ", ";
cout << " ";
}
cout<<" string "<<strData<<" has "<<ro<<" palindromic decompositions";
}



int main()
{
string stringData;
while(1)
{
cout<<" Enter a string: ";


cin>>stringData;
if (stringData.compare("-1") == 0)
break;
allPalPartitions(stringData);
}
return 0;
}


Explanation / Answer

#include <bits/stdc++.h>
#include <iostream>
#include<vector>
using namespace std;
bool isPalindrome(string strData, int first, int last)
{
while (first < last)
{
if (strData[first] != strData[last])
return false;
first++;
last--;
}
return true;
}


void allPalPartUtility(vector<vector<string> >&allPart, vector<string> &currentPart, int startPos, int endPos, string strData)
{
if (startPos >= endPos)
{
allPart.push_back(currentPart);
return;
}
for (int co = startPos; co < endPos; co++)
{
if (isPalindrome(strData, startPos, co))
{
currentPart.push_back(strData.substr(startPos, co - startPos + 1));
allPalPartUtility(allPart, currentPart, co + 1, endPos, strData);
currentPart.pop_back();
}
}
}


void allPalPartitions(string strData)
{
int no = strData.length();
int ro, co;
vector<vector<string> > allPart;
vector<string> currentPart;
allPalPartUtility(allPart, currentPart, 0, no, strData);
cout<<" Palindromic decomposition of string: ";
for (ro = 0; ro < allPart.size(); ro++ )
{
for (co = 0; co < allPart[ro].size(); co++)
cout << allPart[ro][co] << ", ";
cout << " ";
}
cout<<" string "<<strData<<" has "<<ro<<" palindromic decompositions";
}


int main()
{
string stringData;
while(1)
{
cout<<" Enter a string: ";

cin>>stringData;
if (stringData.compare("-1") == 0)
break;
allPalPartitions(stringData);
}
return 0;
}