Convert from C++ to C language. Following is the requirement of program I am try
ID: 3591872 • Letter: C
Question
Convert from C++ to C language.
Following is the requirement of program I am trying to create!! Its a C program and needes to be coded in C Language and not C++. I have posted it several times and everyone gives me a C++ code which is totally unnecessary for me. Also, I have included a C++ version solution for the following program so, can someone please convert the following C++ code to C Language ot create a new program in C langugage, which ever is easy.
Generate all palindromic decompositions of a string s (cut it into substrings that are palindromes).
For example the string abbccd has 4 palindromic decompositions:
Requirements:
In main (or another function that will be called in main), repeatedly read a string from the user, until the user enters -1. For each of these strings, print the palindromic decomposition (one per line) and after that the total number of decompositions.
The function that generates the decompositions and counts them must be recursive.
The maximum string length is 100.
Source code in C++:
// C++ program to print all palindromic partitions of a given string
#include<bits/stdc++.h>
using namespace std;
// A utility function to check if str is palindroem
bool isPalindrome(string str, int low, int high)
{
while (low < high)
{
if (str[low] != str[high])
return false;
low++;
high--;
}
return true;
}
// Recursive function to find all palindromic partitions of str[start..n-1]
// allPart --> A vector of vector of strings. Every vector inside it stores
// a partition
// currPart --> A vector of strings to store current partition
void allPalPartUtil(vector<vector<string> >&allPart, vector<string> &currPart,
int start, int n, string str)
{
// If 'start' has reached len
if (start >= n)
{
allPart.push_back(currPart);
return;
}
// Pick all possible ending points for substrings
for (int i=start; i<n; i++)
{
// If substring str[start..i] is palindrome
if (isPalindrome(str, start, i))
{
// Add the substring to result
currPart.push_back(str.substr(start, i-start+1));
// Recur for remaining remaining substring
allPalPartUtil(allPart, currPart, i+1, n, str);
// Remove substring str[start..i] from current
// partition
currPart.pop_back();
}
}
}
// Function to print all possible palindromic partitions of
// str. It mainly creates vectors and calls allPalPartUtil()
void allPalPartitions(string str)
{
int n = str.length();
// To Store all palindromic partitions
vector<vector<string> > allPart;
// To store current palindromic partition
vector<string> currPart;
// Call recursive function to generate all partiions
// and store in allPart
allPalPartUtil(allPart, currPart, 0, n, str);
// Print all partitions generated by above call
for (int i=0; i< allPart.size(); i++ )
{
for (int j=0; j<allPart[i].size(); j++)
cout << allPart[i][j] << " ";
cout << " ";
}
}
// Driver program
int main()
{
string str = "nitin";
allPalPartitions(str);
return 0;
}
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
// A utility function to check if str is palindroem
bool isPalindrome(string str, int low, int high)
{
while (low < high)
{
if (str[low] != str[high])
return false;
low++;
high--;
}
return true;
}
// Recursive function to find all palindromic partitions of str[start..n-1]
// allPart --> A vector of vector of strings. Every vector inside it stores
// a partition
// currPart --> A vector of strings to store current partition
void allPalPartUtil(vector<vector<string> >&allPart, vector<string> &currPart,
int start, int n, string str)
{
// If 'start' has reached len
if (start >= n)
{
allPart.push_back(currPart);
return;
}
// Pick all possible ending points for substrings
for (int i=start; i<n; i++)
{
// If substring str[start..i] is palindrome
if (isPalindrome(str, start, i))
{
// Add the substring to result
currPart.push_back(str.substr(start, i-start+1));
// Recur for remaining remaining substring
allPalPartUtil(allPart, currPart, i+1, n, str);
// Remove substring str[start..i] from current
// partition
currPart.pop_back();
}
}
}
// Function to print all possible palindromic partitions of
// str. It mainly creates vectors and calls allPalPartUtil()
void allPalPartitions(string str)
{
int n = str.length();
// To Store all palindromic partitions
vector<vector<string> > allPart;
// To store current palindromic partition
vector<string> currPart;
// Call recursive function to generate all partiions
// and store in allPart
allPalPartUtil(allPart, currPart, 0, n, str);
// Print all partitions generated by above call
for (int i=0; i< allPart.size(); i++ )
{
for (int j=0; j<allPart[i].size(); j++)
cout << allPart[i][j] << " ";
cout << " ";
}
}
// Driver program
int main()
{
string str = "nitin";
allPalPartitions(str);
return 0;
}