Monday, July 2, 2018

C palindrome test using pointers

/* program tests a string for palindrome using pointer notation */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(void)
{
  char *palin,c;
  int i=0, count=0;
  printf("Please enter a word \n");
  short int palindrome(char *,int);       /* Function prototype *  has a bug first argument should be char */
  palin = (char*)malloc(20*sizeof(char));
  printf("Enter a word\n");

  do
  {
     c=getchar();
     palin[i]=c;
     i++;
  }while(c != '\n');
  printf("i=%d\n",i );
  i=i-1;
  palin[i]='\0';
  count =i;

  if(palindrome(palin,count)==1)
        printf("Entered word is not a palindrome\n");
  else
        printf("Entered word is  a palindrome\n");


}

short int palindrome(char * palin, int len)
{
    short int i=0,j=0;
    for(i=0;j=len-1,i<len/2;i++,j--)
    {
        if(palin[i]==palin[j])
            continue;
        else
            return(1);
    }
    return(0);

}

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows