C programming The instruction to complete the code is on the picture This is the
ID: 3574707 • Letter: C
Question
C programming
The instruction to complete the code is on the picture
This is the code that need to be completed
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int isPalindrome(const char *str, int left_index, int right_index);
int main(int argc, char **argv) {
if(argc != 2) {
printf("Usage: isPalindrome word ");
exit(1);
}
int isPal = isPalindrome(argv[1], 0, strlen(argv[1])-1);
if(isPal) {
printf("%s is a palindrome! ", argv[1]);
} else {
printf("%s is NOT a palindrome! ", argv[1]);
}
return 0;
}
/**
* This function should return true if the given (sub)string is
* a palindrome. The function should be recursive.
*/
int isPalindrome(const char *str, int left_index, int right_index) {
//TODO: implement this function as specified
}
Activity 1 -Solving a Problem Using Recursion Apalindrome is a string that is the same backward and forward: "rats live on no evil star" or single words such as "civic" or "deed" are palindromes. In this activity you will design and implement a recursive function that determines whether or not a given string is a palindrome. The recursive function should work as follows. The function is given a string, a left-index, and a right-index. If the characters at the left index and right index are not equal then we can stop, it is not a palindrome Lab Handout: Recursion Otherwise, we need to check the substring not including the (equal) characters at the left/right ndices. Until the string is empty (the left/right indices are the same or have crossed each other at which point we can say, yes it is a palindrome For example, given the word "civic" we would check the first and last characters. Finding them the same, we would recursively call the function on the substring Wivi". However, with the proper function parameters, we don't create a new substring: we simply pass the relevant indices that define the substring. Instructions 1. Download palindrome .c and edit it 2. Complete the isPalindrome function as specified above. 3. Compile and test your program from the command line. Note: you can test a phrase by encapsulating it in double quotes; example: /a.out "rats live on no evil star" 4. Demonstrate your working program to a lab instructorExplanation / Answer
I had added the code for your method. :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int isPalindrome(const char *str, int left_index, int right_index);
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: isPalindrome word ");
exit(1);
}
int isPal = isPalindrome(argv[1], 0, strlen(argv[1]) - 1);
if (isPal) {
printf("%s is a palindrome! ", argv[1]);
}
else {
printf("%s is NOT a palindrome! ", argv[1]);
}
return 0;
}
/**
* This function should return true if the given (sub)string is
* a palindrome. The function should be recursive.
*/
int isPalindrome(const char *str, int left_index, int right_index) {
if (NULL == str || left_index < 0 || right_index < 0) {
printf("Invalid Input");
return 0;
}
/* Recursion termination condition */
if (left_index >= right_index)
return 1;
if (str[left_index] == str[right_index]) {
return isPalindrome(str, left_index + 1, right_index - 1);
}
return 0;
}
feel free to ask if you have any doubt :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.