Monday, February 28, 2011

WAP using recursion to Palindrom

/*
* isPalindrome.c
*
*/

#include
#include

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];

result = isPalindrome( "jalaj", 7 ); /* recursive starts here */

if( result == 1 )
{
puts( "Its a palindrome string." );
}
else
{
puts( "Its not a palindrome string." );
}

getchar();
return 0;

}

No comments :