Friday, May 20, 2016

C program to find whether given string is palindrome or not

Problem Statement:- you are given a string and you need to check whether the given string is palindrome or not, if yes you need to produce output yes
Palindrome:- if the string is equal to its reverse of string then it is called palindrome numbers

#include<stdio.h>
#include<string.h>
void check(char *t)
 {
  char *i;
//strlen() is used to calculate length of the string defined in string.h
  int l=strlen(t);
  while(*t!=NULL)
   {
    *i=*t;
    i++;
    t++;
   }
   *i='\0';
   t=t-l;
   i--;
   int count=0;
   while(*t!=NULL)
    {
     if(*t!=*i)
      {
       count++;
       break;
      }
      t++;
      i--;
    }
  if((count==0)&&(*t==NULL))
   {
    printf("String is palindrome");
   }
 else
  {
   printf("String is not palindrome");
  }
}

void main()
{
 char s[20];
 printf("Enter a string");
 scanf("%s",s);
 check(s);
}

output
Enter a string
swswwsws
String is palindrome

No comments:

Post a Comment