Monday, March 23, 2015

C program to check palindrome

Palindrome is a string,which when read in both forward and backward way in same.
Example:-madam,radar,rotavator

#include<stdio.h>
char string_palin(char str[]);

int main()
{
 char str[30];
 clrscr();
 printf("Enter string : ");
 scanf("%s",&str);

 if(string_palin(str))
    printf("Entered string Palindrome");
 else
    printf("Entered string not Palindrome");
    getch();
 return 0;
}

/*function for palindrome*/

char string_palin(char str[])
{
 int i,j;
 for(i=0; str[i]!=NULL; i++);
 for(j=0,i--; j<=i; )
 {
   if(str[i]==str[j])
   {
      i--;
      j++;
   }
  else
      break;
 }
 if(j>i)
    return(1);
 else
    return(0);
}

C program to reverse string using recursion

#include<stdio.h>
void reverse(char*,int,int);

int main()
{
char a[100];
gets(a);
reverse(a,0,strlen(a)-1);
printf("%s\n",a);
return 0;
}

void reverse(char *x,int i,int j)
{
char c;
if(i>=j)
return;
c=*(x+i);
*(x+i)=*(x+j);
*(x+j)=c;

reverse(x,++i,--j);
}