C programming, plz show the output A palindrome is a string that reads the same
ID: 3684686 • Letter: C
Question
C programming, plz show the output
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 is Palindrome 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 isPalindrome Flexible 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>
int isPalindrome(char *str)
{
int i,j,len;
for(len=0;str[len]!='';)len++;
for(i=0,j=len-1;i<=j;i++,j--)
{
if(str[i]!=str[j])
{
return 0;
}
}
return 1;
}
int isPalindromeFlexible(char *str)
{
int i,j,len;
for(len=0;str[len]!='';)len++;
//printf("%d ",len);
for(i=0,j=len-1;i<=j;)
{
if(str[i]==' ')
{
i++;
continue;
}
if(str[j]==' ')
{
j--;
continue;
}
if(str[i]>='A' && str[i]<='Z')
{
str[i] = str[i]-'A'+'a';
}
if(str[j]>='A' && str[j]<='Z')
{
str[j] = str[j]-'A'+'a';
}
if(str[i]!=str[j])
{
return 0;
}
i++;j--;
}
return 1;
}
void spaces(int m)
{
int i;
for(i=0;i<m;i++)printf(" ");
}
int main(void) {
// your code goes here
int k;
printf("String");
spaces(29);
printf("|");
printf("isPalindrome");
printf("|");
printf("isPalindromeFlexible ");
for(k=0;k<70;k++)printf("-");
printf(" ");
char ch[] ="Ott o";
printf("%s",ch);
spaces(35-strlen(ch));
printf("|");
k = isPalindrome(ch);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
char ch1[] ="tacocat";
printf("%s",ch1);
spaces(35-strlen(ch1));
printf("|");
k = isPalindrome(ch1);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch1);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
char ch2[] ="Mirror";
printf("%s",ch2);
spaces(35-strlen(ch2));
printf("|");
k = isPalindrome(ch2);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch2);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
char ch3[] ="123 3 2 1";
printf("%s",ch3);
spaces(35-strlen(ch3));
printf("|");
k = isPalindrome(ch3);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch3);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
char ch4[] ="A santa lived as a devil at NASA";
printf("%s",ch4);
spaces(35-strlen(ch4));
printf("|");
k = isPalindrome(ch4);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch4);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
char ch5[] ="anna_bob";
printf("%s",ch5);
spaces(35-strlen(ch5));
printf("|");
k = isPalindrome(ch5);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch5);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
char ch6[] =" c";
printf("%s",ch6);
spaces(35-strlen(ch6));
printf("|");
k = isPalindrome(ch6);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch6);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
char ch7[] =" c ";
printf("%s",ch7);
spaces(35-strlen(ch7));
printf("|");
k = isPalindrome(ch7);
if(k==1)
{
printf("YES");
spaces(9);
printf("|");
}
else
{
printf("NO");
spaces(10);
printf("|");
}
k = isPalindromeFlexible(ch7);
if(k==1)
{
printf("YES");
spaces(17);
}
else
{
printf("NO");
spaces(18);
}
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.