Showing posts with label Palindrome. Show all posts
Showing posts with label Palindrome. Show all posts

Thursday, July 21, 2011

Program to check for palindrome

/*Serial No.101     [swami77.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,len,flag;
char str[10],rev[10];
clrscr();
printf("Enter the string==>");
scanf("%s",str);
len=strlen(str);
printf("\nLength of %s is ==>%d\n",str,len);
for(i=0;str[i]!='\0';i++)
    {
    if(str[i]!=str[len-1-i])
      {
      flag=3;
      break;
      }
    }
if(flag==3)
    printf("\nString is not polyndrom");
     else
    printf("\nString is polyndrom");
getch();
}

Wednesday, July 20, 2011

Program to check palindrome using recursive function

/*Serial No.96     [swami71.cpp]*/ 

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void reverse(char num[],int a,int b)
    {
    if(a<b)
       {
       if(num[a++]==num[b--])
       reverse(num,a,b);
       else
         {
         printf("not a palindrome.\n");
         getch();
         exit;
         }
       }
    else
      {
      printf("palindrome");
      getch();
      exit(0);
      }
      }
void main()
{
int len;
char num[6];
printf("\n enter the no.");
scanf("%s",num);
len=strlen(num);
len--;
reverse(num,0,len);
}

Program to find the length, reverse of a string and to check it is palindrome or not

/*Serial No.52     [swami24.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,length=0,flag;
char str[10],rev[10];
clrscr();
printf("Enter the string==>");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
printf("\nLength of %s is ==>%d\n",str,length);
length--;
for(i=0;length>=0;i++)
{
 rev[i]=str[length];
 length--;
}
rev[i]='\0';
printf("\nReverse of %s is ==>%s\n",str,rev);

 length++;
for(i=0;rev[i]!='\0';i++)
{
length++;
}
printf("\nLength of %s is ==>%d\n",rev,length);

for(i=0;str[i]!='\0'||rev[i]!='\0';i++)
{
if(str[i]!=rev[i])
    {
    flag=1;
    break;
    }
}
if(flag==1)
printf("\nString is not polyndrom");
else
printf("\nString is polyndrom");
getch();
}