In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of April 14th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #06 exercise sheet]
In this problem you should submit a file containing the function as described (without any main function). Inside the function do not print anything that was not asked!
Chandler Bing has just learned about palindromes, and now he can't stop testing words. A palindrome is a word that reads the same forwards and backwards, like "dad", "racecar" or "wow".
Now, Chandler is convinced that he can outsmart everyone with his hilarious word choices. But before he embarrasses himself in front of Monica, he needs to be absolutely sure his words are real palindromes.
Write a function void int palindrome(char s[], int start, int end) that receives a string s[] and two indices start and end. It should return 1 if the string between positions start and end is a palindrome and 0 if it is not.
Note: although this function can be written in a iterative way (using a cycle) our proposal in this task is that you try to find a recursive definition.
The following limits are guaranteed in all the test cases that will be given to your program:
1 ≤ |s| < 100 | Size of the string | |
0 ≤ start ≤ end ≤ |s| | Indices to consider |
You should submit a .c
file containing the requested function, without any main function and without printing anything. You can however create additional methods, if you need them.
Mooshak will use the following code to link to your function, read the inputs and call your method, printing its result.
#include <stdio.h> #include <string.h> #define MAX_SIZE 100 int palindrome(char [], int, int); int main(void) { // Read amount of test cases int n; scanf("%d", &n); // Read n cases and for each call the palindrome function char str[MAX_SIZE]; for (int i=0; i<n; i++) { scanf("%s", str); int n = strlen(str); printf("palindrome(%s, 0, %d) = %d\n", str, n-1, palindrome(str, 0, n-1)); } return 0; }
Example Input | Example Output |
5 wow racecar c python abcdba |
palindrome(wow, 0, 2) = 1 palindrome(racecar, 0, 6) = 1 palindrome(c, 0, 0) = 1 palindrome(python, 0, 5) = 0 palindrome(abcdba, 0, 5) = 0 |