Please write code in C. Thank you!
A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. You may assume that the input string will not exceed 50 characters.
Ex: If the input is bob, the output is:
bob is a palindrome
Ex: If the input is bobby, the output is:
bobby is not a palindrome
Hint: Start by just handling single-word input, and submit for grading. Once passing single-word test cases, extend the program to handle phrases. If the input is a phrase, remove or ignore spaces.
#include <stdio.h> #include<string.h> int isPalindrome(char arr[]){ int size = strlen(arr); int i = 0,j = size-1; for(i= 0;i<j;){ if(arr[i]==' '){ i++; continue; } if(arr[j] == ' '){ j--; continue; } if(arr[i] != arr[j]){ return 0; } i++; j--; } return 1; } int main() { int n,i; char arr[50]; scanf(" %[^\n]s",arr); if(isPalindrome(arr) == 1) printf("%s is a palindrome\n",arr); else printf("%s is not a palindrome\n",arr); return 0; }
Get Answers For Free
Most questions answered within 1 hours.