code: /***************************************************************** * Progr
ID: 3754779 • Letter: C
Question
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
/*****************************************************************
* 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[^ ]", &buffer);
int length = strlen(buffer); /* length of string up to first null-character */
printf("The string is %sa palindrome. ",
is_palindrome(buffer, 0, length-1) ? "" : "not ");
return EXIT_SUCCESS;
}
Explanation / Answer
// A recursive C program to
// check whether a given number
// is palindrome or not
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// A recursive function that
// check a str[s..e] is
// palindrome or not.
bool isPalRec(char str[],
int s, int e)
{
// If there is only one character
if (s == e)
return true;
// If first and last
// characters do not match
if (str[s] != str[e])
return false;
// If there are more than
// two characters, check if
// middle substring is also
// palindrome or not.
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
bool isPalindrome(char str[])
{
int n = strlen(str);
// An empty string is
// considered as palindrome
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
// Driver Code
int main()
{
char str[] = "geeg";
if (isPalindrome(str))
printf("Yes");
else
printf("No");
return 0;
}
// A recursive C program to
// check whether a given number
// is palindrome or not
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// A recursive function that
// check a str[s..e] is
// palindrome or not.
bool isPalRec(char str[],
int s, int e)
{
// If there is only one character
if (s == e)
return true;
// If first and last
// characters do not match
if (str[s] != str[e])
return false;
// If there are more than
// two characters, check if
// middle substring is also
// palindrome or not.
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
bool isPalindrome(char str[])
{
int n = strlen(str);
// An empty string is
// considered as palindrome
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
// Driver Code
int main()
{
char str[] = "geeg";
if (isPalindrome(str))
printf("Yes");
else
printf("No");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.