Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A palindrome is a string that reads the same both forward and backward. For exam

ID: 3685192 • 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: ( represents a space)

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <ctype.h>

// A function to check if a string str is palindrome
void isPalindrome(char* str)
{
   // Start from leftmost and rightmost corners of str
   int l = 0;
   int h = strlen(str) - 1;

   // Keep comparing characters while they are same
   while (h > l)
   {
       if (str[l++] != str[h--])
       {
           printf("%s is Not Palindrome ", str);
           return;
       }
   }
   printf("%s is palindrome ", str);
}
void isPalindromeFlexible(char* str)
{
   // Start from leftmost
   int l = 0;
   int h = strlen(str) - 1;

   // Keep comparing characters while they are same
   while (h > l)
   {
       if (tolower(str[l]) != tolower(str[h]))
       {
           if(str[l] == ' '){
           l++;
           continue ;
           }
           if(str[h] == ' '){
           h--;
           continue ;
           }
           printf("%s is Not Palindrome ", str);
           return;
       }
       l++;
       h--;
   }
   printf("%s is pali
ndrome ", str);
}
// Driver program to test above function
int main()
{
   isPalindromeFlexible("A bba");
   isPalindrome("A bba");
   isPalindrome("abbccbba");
   isPalindrome("geeks");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote