Thursday, May 27, 2010

A C Program to find the given sring is Palindrome or not Without using String Functions

#include"iostream.h"
#include"stdio.h"
#include"conio.h"

void main()
 {
  int i=0,j=0,n=0,cnt=0;
  char a[20],b[20];
  clrscr();

  printf(" ENTER THE STRING : ");
  cin>> a >> b;

  //TO FIND THE LENGTH WITHOUT USING STRLEN() FUNCTION
  while(a[n] != '\0')
   n = n+1;

  //TO REVERSE THE STRING WITHOUT USING STRREV() FUNCTION
  for(i=n-1;i > = 0 ; i--)
  {
   b[j] = a[i];
   j++;
  }

  //TO COMPARE THE STRINGS WITHOUT USING STRCMP() FUNCTION
  for( i = 0;i < n; i ++)
  {
   if(a[i] == b[i])
   {
    cnt++;
   }
   else
   {
    printf("GIVEN STRING IS NOT A PALINDROME");
    break;
   }
  }

   if(cnt == n)
   {
    printf("GIVEN STRING IS A PALINDROME");
   }
  getch();
}

No comments:

Post a Comment