C PROGRAMMING, PLZ HELP! A palindrome is a string that reads the same both forwa
ID: 3684602 • Letter: C
Question
C PROGRAMMING, PLZ HELP!
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:Explanation / Answer
#include<stdio.h>
#include<string.h>
const char * arr[] = {
"Ott o",
"tacocat",
"Mirror",
"123 3 2 1",
"A santa lived as a devil at NASA",
"anna_bob",
" c",
" c ",
};
//checks given string palindrome or not
int isPalindrome(char* str)
{
//variables declared
int i=0,length=0;
//getting length
while(str[i] != ''){
length++;
i++;
}
//checking from start and end characters upto middle
for( i = 0 ; i < length / 2 ; i++ )
{
if( *(str + i ) != *(str + length - i - 1) )
return 0;
}
return 1;
}
int isPalindromeFlexible(char* str)
{
//variables declared
int i=0,length=0;
while(str[i] != ''){ //getting length
length++;
i++;
}
//removing spaces and converting to lower case
for (i = 0; i < length; ) {
str[i] = tolower(str[i]);
if (isspace(str[i])) {
remove(str[i]);
} else {
i++;
}
}
//valling palindrome check function
return isPalindrome(str);
}
void main()
{
//printing menu
printf("string | isPalindrome | isPalindromeFlexible ");
printf(" ----------------------------------------------------------------");
int i;
//calling functions and printing results
for (i = 0; i < 8; i++) {
printf (" %s |",arr[i]);
if(isPalindrome(arr[i]) == 1){
printf(" yes");
}
else{
printf(" No");
}
if(isPalindromeFlexible(arr[i]) == 1){
printf(" | yes");
}
else{
printf(" | No");
}
}
printf(" Ott o | ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.