PLEASE THE PROGRAM SHOULD BE IN C LANGUAGE!!! NOT C++!! I REPEAT THE PROGRAM SHO
ID: 3591791 • Letter: P
Question
PLEASE THE PROGRAM SHOULD BE IN C LANGUAGE!!! NOT C++!! I REPEAT THE PROGRAM SHOULD BE IN C LANGUAGE
Generate all palindromic decompositions of a string s (cut it into substrings that are palindromes). For example the string abbccd has 4 palindromic decompositions:
a, b, b, c, c, d,
a, b, b, cc, d,
a, bb, c, c, d,
a, bb, cc, d,
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.
write the recurrence formula (T(n) = .... and the base case T(...) = ... ) for the recursive function that generates the palindromes as a function of the string length, N
Explanation / Answer
#include <stdio.h>
#include <string.h>
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int l, int r)
{
int i;
if (l == r)
printf("%sn", a);
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permute(a, l+1, r);
swap((a+l), (a+i));
}
}
}
int main()
{
char str[] = "ABC";
int n = strlen(str);
permute(str, 0, n-1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.