#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' };
printf( "Please type the string to identify Palindrom or not: ", 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 )
{
printf( "1" );
}
else
{
printf( "0" );
}
getchar();
return 0;
}
I wrote this code in c++ but now I wanna switch it to c...It works perfectly fine in c++..
But I kind a forget c functions. Can someone help me and building a code for me in c???