Could someone please walk me through this assignmenrt and explain what is happen
ID: 3686360 • Letter: C
Question
Could someone please walk me through this assignmenrt and explain what is happening?
Write a program called “recursive_algos.cpp” that contains two recursive and one nonrecursive functions
1. A recursive boolean function called palindrome that accepts one string argument and returns true if the string reads the same forward as it does backwards. For example madam, 463364, and “ABLE WAS I ERE I SAW ELBA” are all palindromes
2. A recursive function called printReverse that accepts one string argument and returns the reverse of that string.
3. A nonrecursive function called printIterReverse that accepts one string argument and returns the reverse of that string
Consider the following prototypes for the functions:
bool palindrome(string & s);
string printReversel(string & s);
string printIterReverse(string & s);
Explanation / Answer
Hi below i have written the sample code for all the program using the recursive and non-recursive method for your reference,
1) A recursive boolean function called palindrome that accepts one string argument and returns true if the string reads the same forward as it does backwards
// Recursive program to check if a given linked list is palindrome
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* Link list node */
struct node
{
char data;
struct node* next;
};
// Initial parameters to this function are &head and head
bool isPalindromeUtil(struct node **left, struct node *right)
{
/* stop recursion when right becomes NULL */
if (right == NULL)
return true;
/* If sub-list is not palindrome then no need to
check for current left and right, return false */
bool isp = isPalindromeUtil(left, right->next);
if (isp == false)
return false;
/* Check values at current left and right */
bool isp1 = (right->data == (*left)->data);
/* Move left to next node */
*left = (*left)->next;
return isp1;
}
// A wrapper over isPalindromeUtil()
bool isPalindrome(struct node *head)
{
isPalindromeUtil(&head, head);
}
/* Push a node to linked list. Note that this function
changes the head */
void push(struct node** head_ref, char new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to pochar to the new node */
(*head_ref) = new_node;
}
// A utility function to print a given linked list
void printList(struct node *ptr)
{
while (ptr != NULL)
{
printf("%c->", ptr->data);
ptr = ptr->next;
}
printf("NULL ");
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
char str[] = "abacaba";
int i;
for (i = 0; str[i] != ''; i++)
{
push(&head, str[i]);
printList(head);
isPalindrome(head)? printf("Is Palindrome "):
printf("Not Palindrome ");
}
return 0;
}
2) A recursive function called printReverse that accepts one string argument and returns the reverse of that string.
// C program to reverse a string using recursion
# include <stdio.h>
/* Function to print reverse of the passed string */
void reverse(char *str)
{
if (*str)
{
reverse(str+1);
printf("%c", *str);
}
}
/* Driver program to test above function */
int main()
{
char a[] = "Geeks for Geeks";
reverse(a);
return 0;
}
3.A nonrecursive function called printIterReverse that accepts one string argument and returns the reverse of that string
// C program to reverse a string using recursion
# include <stdio.h>
/* Function to print reverse of the passed string */
void reverse(char *str)
{
if (*str)
{
reverse(str+1);
printf("%c", *str);
}
}
/* Driver program to test above function */
int main()
{
char a[] = "Geeks for Geeks";
reverse(a);
return 0;
}
3.A nonrecursive function called printIterReverse that accepts one string argument and returns the reverse of that string
#include<stdio.h> #include<conio.h> void main() { int i, j, k; char str[100]; char rev[100]; printf("Enter a string "); scanf("%s", str); printf("The original string is %s", str); for(i = 0; str[i] != ''; i++); { k = i-1; } for(j = 0; j <= i-1; j++) { rev[j] = str[k]; k--; } printf("The reverse string is %s", rev); getch(); } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.