Showing posts with label palindrome. Show all posts
Showing posts with label palindrome. Show all posts

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

Monday, March 28, 2016

C program to find longest palindrome string from a string

#include<stdio.h>
#include<string.h>
int check_pal(char str_check[],int i,int j)
{
 int k,flag=0;
 for(k=0;k<=(j-i)/2;k++)
  {
   if(str_check[i+k]!=str_check[j-k])
    {
     flag=1;
     break;
    }
  }
 if(flag==1)
  {
   return -1;
  }
 else
  {
   return j-i+1;
  }
}
void main()
{
 char *str="vhgfhjggjjggknkjnk";
 int i,j,count=0,max=0,start,end,length;
 for(i=0;str[i]!='\0';i++)
  {
   for(j=i+1;str[j]!='\0';j++)
    {
     if(str[i]==str[j])
      {
       length=check_pal(str,i,j);
       if(length>max)
{
start=i;
end=j;
max=length;
}
      }
    }
   }
 printf("Length =%d\n",max);
 printf("Longest substring is as follows ");
 for(i=start;i<=end;i++)
  {
   printf("%c",str[i]);
  }
}

output
Longest substring is as follows ggjjgg

Saturday, March 12, 2016

C program to check the value is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int n,m,sum=0,a;
  printf("Enter a no");
  scanf("%d",&n);
//a integer number is palindrome if original number and number formed by reversing the sequence of //digits are equal
  m=n;
  while(n>0)
   {
    a=n%10;
    sum=sum*10+a;
    n=n/10;
   }
  if(sum==m)
   {
    printf("No is palindrome");
   }
  else
   {
    printf("No is not palindrome");
   }
 getch();
}

output
Enter a no 242
No is palindrome