Saturday, December 5, 2009

PROGRAM TO CHECK PALINDROME OF STRING USING RECURRSION

#include "stdio.h"
#include "conio.h"
#include "string.h"

int palindrome(char *string)
{
int len;
char first, last;
len = strlen(string);
if(len == 1 || len == 0)
return 1; //palindrome

first = string[0]; //first character of string
last = string[len-1];//last character of string
if(first == last)
{
/*delete first and last characters -- extract the middle
portion of string */

for(int i=0; i string[i] = string[i+1];//this deletes first char

//now delete last char
string[len-2] = '\0';

return palindrome(string);
}

return 0; //not palindrome
}


int main()
{
char str[30];
clrscr();
printf("\nEnter a string: ");
gets(str);

if(palindrome(str))
printf("\nPalindrome.");
else
printf("\nNot palindrome");

getch();
return 0;
}

0 comments:


by Ankit Pokhrel.