Question: code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining * if a string is a palindrome * * Authors: Steven

 code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive

function for determining * if a string is a palindrome * *

code:

/***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining * if a string is a palindrome * * Authors: Steven R. Vegdahl, Tammy VanDeGrift, Martin Cenek * *****************************************************************/ #include #include #include /***************************************************************** * is_palindrome - determines whether a string of characters is a palindrome * * calling sequence: * result = is_palindrome(str, first_index, last_index) * * parameters - * str - the string to test * first_index - index of the first character in the string * last_index - index of the last character of the string * * result - * Returns a 1 if the string in the range first_index..last_index is a * palindrome; otherwise returns a 0. * * example - * If idx1 is 0 and idx2 is 4, and str is "radar in range", then a 1 is * returned; if the string were "rural", then 0 is returned. * * implementation - * The first and last characters are examined. If they don't match, * a zero is returned. Otherwise we (recursively) test the string * without the first and last characters. * *****************************************************************/ int is_palindrome(char *str, int first_index, int last_index) { if (first_index >= last_index) { /* BASE CASE: string of length 1 or less: return 1 for true */ return 1; } else if (str[first_index] != str[last_index]) { /* BASE CASE: first and last chars mismatch: return 0 for false */ return 0; } else { /* RECURSIVE CASE: str[first_index] == str[last_index] */ /* store result in temporary variable so that we can look at it in debugger */ int result = 0; // update this to be the result of the recursive call // should just be one line of code return result; } } /***************************************************************** * main - main program to exercise 'is_palindrome' * * This program prompts the user a string, and then calls 'is_palindrome' on the * string. * note: only works for strings of

Part 2: Palindrome test (palindrome.c) - Fix the recursive case 1. Examine the is_palindrome function in palindrome.c. This function is supposed to determine whether a string is a palindrome (i.e., whether it reads the same backward as forward). The function takes three parameters: a string palindrome considered part of the palindrome the index in the string of the first character to be considered to be part of the the index in the string of the last character in the string that is to be 2. The algorithm is as follows: base case #1 (already done): the first index is greater than or equal to the last index. In this case we have a one-character or zero-character string, which is a palindrome, so return 1. base case #2 (already done): the first index is less than the last index, but the respective characters do not match. In this case return 0. recursive case (which you need to do): the first index is less than the last index, but the values at these character positions are equal. We have a palindrome only if we get a palindrome after discarding the first and last character of the string, so we can simply return the recursive call to is palindrome on this shorter string (shorter by 2 characters). . The algorithm should always terminate, because at each recursive call, we are calling is _palindrome on a shorter substring. 3. Modify the recursive case so that it runs correctly. All other code should remain the same. Compile and run the program gcc -g-o palindrome palindrome.c palindrome Once you are confident it is working, use the debugger to run the program. Set a breakpoint at the first line of code inside is_palindrome. gdb palindrome (gdb) rurn Find the first line of code inside is_palindrome. Set a breakpoint there using the break command. Then, use a combination of cont and info stack to view the stack contents each time the breakpoint is hit (each time the function is recursively called) Part 2: Palindrome test (palindrome.c) - Fix the recursive case 1. Examine the is_palindrome function in palindrome.c. This function is supposed to determine whether a string is a palindrome (i.e., whether it reads the same backward as forward). The function takes three parameters: a string palindrome considered part of the palindrome the index in the string of the first character to be considered to be part of the the index in the string of the last character in the string that is to be 2. The algorithm is as follows: base case #1 (already done): the first index is greater than or equal to the last index. In this case we have a one-character or zero-character string, which is a palindrome, so return 1. base case #2 (already done): the first index is less than the last index, but the respective characters do not match. In this case return 0. recursive case (which you need to do): the first index is less than the last index, but the values at these character positions are equal. We have a palindrome only if we get a palindrome after discarding the first and last character of the string, so we can simply return the recursive call to is palindrome on this shorter string (shorter by 2 characters). . The algorithm should always terminate, because at each recursive call, we are calling is _palindrome on a shorter substring. 3. Modify the recursive case so that it runs correctly. All other code should remain the same. Compile and run the program gcc -g-o palindrome palindrome.c palindrome Once you are confident it is working, use the debugger to run the program. Set a breakpoint at the first line of code inside is_palindrome. gdb palindrome (gdb) rurn Find the first line of code inside is_palindrome. Set a breakpoint there using the break command. Then, use a combination of cont and info stack to view the stack contents each time the breakpoint is hit (each time the function is recursively called)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!