Answer to Question #202623 in C for Sia

Question #202623

Write a function isPalindrom() that takes as input a C string and its size, and recursively tests whether it is a palindrome (a word or phrase which remains the same if reversed. e.g. radar, kayak etc). The function returns the Boolean data type which is either true or false. The required header “stdbool.h” has already been included in tasks.h.

The function has the following prototype:

bool isPalindrom(char * ptr_array, int size);

The recursive definition of a palindrome is:

• The string is a palindrome if it has only one character or is an empty string.

• The string is a palindrome if the first and the last characters are the same and the characters in between form a palindrome.



1
Expert's answer
2021-06-04T06:09:24-0400
#include <stdio.h>

int main()
{
  char text[100];
  int begin, middle, end, length = 0;

  gets(text);

  while (text[length] != '\0')
    length++;

  end = length - 1;
  middle = length/2;

  for (begin = 0; begin < middle; begin++)
  {
    if (text[begin] != text[end])
    {
      printf("Not a palindrome.\n");
      break;
    }
    end--;
  }
  if (begin == middle)
    printf("Palindrome.\n");

  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS