i can write a palindrome pro but i dont to write using recursive function can any body help
and to check whether a palindrome can fotmed from a given string?
j=0,k=0;
while((j<strlen(str)-1)/2)
{
if(str[j]!=str[strlen(str)-1-j])
{
k=1;
break;
}
j++;
}
if(k==0)
printf("palindrome")
else
printf(not a palindrome");

Recommended Answers

All 5 Replies

Just one of many ways of doing it:

/*
 * isPalindrome.c
 *
 */

#include <stdio.h>
#include <ctype.h>

int isPalindrome( char *str, int length )
{
   
   if ( length < 1 )
   {
      return 1; /* no more chars to compare, its a palindrome */
   }

   if ( str[0] == str[length-1] )            /* Are the two ends same? */
   {
      return isPalindrome( str+1, length-2 ); /* continue checking */
   }
   else
   {
      return 0;      /* comparison failed, not a palindrome */
   }
}

void strToUpper( char *src )
{
   /* convet to upper case any letter */
   while( ( *src = toupper( *src ) ) != '\0' )
   {
      ++src;
   }
}

int main( void )
{
   int result = 0;
   char str[40] = { '\0' };
   
   fputs( "Enter text: ", stdout );
   fflush( stdout );
   
   fgets( str, sizeof str, stdin );
   strToUpper( str );   /* make all letters the same for comparison */
   result = isPalindrome( str, ( strlen( str ) - 1 ) ); /* recursive starts here */
   
   if( result == 1 )
   {
      puts( "Its a palindrome string." );
   }
   else
   {
      puts( "Its not a palindrome string." );
   }
      
   getchar();
   return 0;

 }

/* input: Sator Arepo Tenet Opera Rotas */
/* output: Its a palindrome string. */

This code is not case sensitive. But it doesn't check for puntuation marks nor spaces.

pavani2006,
Even when thekashyap feels that this is a complete solution, I don't want you to think it is. In order to work properly every time, you need to apply a filter that will eliminate spaces, punctuation marks, and basically anything that is not a letter.
The function strToUpper() shows you an example of something like that.
Here is a link to more discussion about this topic: http://www.daniweb.com/techtalkforums/showthread.php?t=59789&page=2&highlight=palindrome+recursive

pavani2006,
Even when thekashyap feels that this is a complete solution, I don't want you to think it is. In order to work properly every time, you need to apply a filter that will eliminate spaces, punctuation marks, and basically anything that is not a letter.
The function strToUpper() shows you an example of something like that.
Here is a link to more discussion about this topic: http://www.daniweb.com/techtalkforums/thread59789.html&page=2&highlight=palindrome+recursive

it is showing that the page doest exit in the server

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.