Wednesday, December 9, 2015

Find a String is Palindrome or not in C Program

Program #15

Description:
To find a given string is PALINDROME or not

What is palindrome?

DUMMY != YMMUD
PALINDROME != EMORDNILAP

LIRIL   = LIRIL
AMMA = AMMA

If a string and its reversed string is same then that string is a "Palindrome".

#include<stdio.h>
void main(){
 char palind[50], compar[50];
 int i,j=0, IsPalind;

 clrscr();
 printf("Please Enter a String to find Palidrome or not?\n");
 scanf("%s", &palind);

 for(i=strlen(palind)-1; i>=0; i--){
   /* Assign last char of Input string into first position of Compare array */
   compar[j]=palind[i];
   j++;
 }

 /* Compare both array to find Palindrome or not */
 IsPalind = 1;
 for(i=0; i<strlen(palind); i++){
   if (palind[i] != compar[i] ){
      IsPalind = 0;
      /* If single character not match, go out of the Loop*/
      break;
   }
 }

 printf("\n\nResult:\n");
 printf("-------\n");

 if (IsPalind){
   printf("Input String is a Palindrome !!");
 } else {
   printf("Invalid Input, Better luck next time !!");
 }

 getch();
}

No comments:

Post a Comment

Popular Posts