Question

Please read the problem carefully and answer the 2 questions below

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)

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 < 10000 characters
* DO NOT MODIFY
*****************************************************************/
int main(int argc, char *argv[]) {
  char buffer[10000]; /* buffer, hopefully plenty big to hold the string */
  buffer[0] = 0;      /* (in case empty string is typed) */
  printf("Please type a line of text: ");
  scanf("%9999[^\n]", &buffer);
  
  int length = strlen(buffer); /* length of string up to first null-character */
  printf("The string is %sa palindrome.\n",
      is_palindrome(buffer, 0, length-1) ? "" : "not ");

  return EXIT_SUCCESS;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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
    result = is_palindrome(str, first_index+1, last_index-1);           

    return result;
  }
}
Add a comment
Know the answer?
Add Answer to:
Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT