A palindrome is a string that reads the same both forward and backward. For exam
ID: 3685233 • Letter: A
Question
A palindrome is a string that reads the same both forward and backward. For example, "tacocat" and "C" are palindromes, "cat" is not. Write a function isPalindrome that takes a string as parameter and returns 1 if the string is a palindrome and 0 otherwise. Your function must take char *str as a parameter. You should not input the length of the string as a second parameter. The end of the string should be detected as the character ''. Write another function isPalindromeFlexible to test if a string is a palindrome ignoring blanks and case mismatch in the matching process. Under these rules, "Anna", "Ott o" are palindromes. Write a program pal.c to test both functions you wrote. Your program should initialize the strings listed in the table below and run isPalindrome and isPalindromeFlexible on each of them. Your program should fill and print the following table, listing each string and the result of the functions on It: (I I represents a space)Explanation / Answer
#include <stdio.h>
#include <string.h>
int isPalindrome (char *str)
{
static int length = strlen (str);
if (length<1)
return 1;
if (str[0] == str[length-1])
{
length -= 2;
return isPalindrome (str+1);/*Recursive call as the function isPalindrome
is called again within itself*/
}
else return 0;
}
write a c language function ispalindrome that takes a string as parameter and returns 1 if the string is palindrome and 0 otherwise. your function must take char*str as a parameter.
int main (void)
{
int result;
char str[256];
printf (" Please type a string: ");
gets (str);/*Input a string to check whether it is a palindrome or not*/
result = isPalindrome (str);/*The function isPalindrome is called.It takes a string
argument and returns a value 0 or 1 which is stored in
the integer variable "result"*/
if (result==1)
printf (" ******Input string is a palindrome string.************ ");
else
printf (" ******Not a palindrome****** ");
return 1;
}
write a c language function ispalindrome flexible to test if a string is a palindorme ignoe blanks and case mismatch in the matching progress under these rules, "Anna" , "Ott o" are palnidromes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STR_LEN 80
typedef enum { false = 0, true } bool;
void erase(char *, size_t);
bool isPalindrome(const char *, const char *);
char in[MAX_STR_LEN];
int main(int argc, char *argv[]) {
size_t x;
while (1) {
printf("> ");
fgets(in,MAX_STR_LEN,stdin);
/*
* Remove trailing newline from entered string, then
* remove all spaces and punctuation from string, and
* convert all to lower case.
*/
*(strchr(in,' ')) = '';
for (x = 0; x < strlen(in); ) {
in[x] = tolower((int)in[x]);
if (isspace((int)in[x]) || ispunct((int)in[x])) {
erase(in, x);
} else {
x++;
}
}
/*
* Check if the result is a palindrome.
*/
printf(""%s" is ",in);
if (isPalindrome(in, in + strlen(in) - 1) == false) {
printf("not ");
}
printf("a palindrome. ");
}
return 0;
}
bool isPalindrome(const char *p, const char *q) {
if (q < p) return true;
if (*q != *p) return false;
return isPalindrome(p + 1, q - 1);
}
void erase(char *s, size_t x) {
memmove(&s[x], &s[x+1], strlen(s) - x);
}
write a program using functions ispalindrome and ispalindromeflexible
#include <stdio.h>
#include <string.h>
int isPalindrome (char *str)
{
void erase(char *, size_t);
bool isPalindrome(const char *, const char *);
char in[MAX_STR_LEN];
static int length = strlen (str);
if (length<1)
return 1;
if (str[0] == str[length-1])
{
length -= 2;
return isPalindrome (str+1);/*Recursive call as the function isPalindrome
is called again within itself*/
}
else return 0;
while (1) {
printf("> ");
fgets(in,MAX_STR_LEN,stdin);
/*
* Remove trailing newline from entered string, then
* remove all spaces and punctuation from string, and
* convert all to lower case.
*/
*(strchr(in,' ')) = '';
for (x = 0; x < strlen(in); ) {
in[x] = tolower((int)in[x]);
if (isspace((int)in[x]) || ispunct((int)in[x])) {
erase(in, x);
} else {
x++;
}
}
/*
* Check if the result is a palindrome.
*/
printf(""%s" is ",in);
if (isPalindrome(in, in + strlen(in) - 1) == false) {
printf("not ");
}
printf("a palindrome. ");
}
return 0;
}
bool isPalindrome(const char *p, const char *q) {
if (q < p) return true;
if (*q != *p) return false;
return isPalindrome(p + 1, q - 1);
}
void erase(char *s, size_t x) {
memmove(&s[x], &s[x+1], strlen(s) - x);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.