#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???

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them

May be but I m not familiar or learned get or put yet. Is there any other way to do it???

May be but I m not familiar or learned get or put yet. Is there any other way to do it???

Then google for those functions and find out what they do. If you want to program then you need to learn how to do your own research.

Edited 5 Years Ago by Ancient Dragon: n/a

May be but I m not familiar or learned get or put yet. Is there any other way to do it???

So? Why do you need get and put ? What needs to change in your code to make it C rather than C++?

No you don't. You want to use fgets() to keep it safe. Here's why

And if the code already works, why change it to be simpler? You will (undoubtedly) break it and have a difficult time fixing it.

I gues you dont know...
Well C++ is part of C, you can write C/C++ Programs, you can mix those 2 and will not be any problem for compiler.

:)

Edited 5 Years Ago by Panathinaikos22: n/a

Comments
has nothing to do with his question.

Ya I always do this kind of thing... I guess the only difference between C and C++ is the way they handle output and input. C++ has cout and cin (which I find simpler than scanf and printf in C). So why try to change the code when only the Input Output is different...

Still if you want to do so,
convert all cout statements to printf()


But my suggestion is to keep your code as it is....

Ya I always do this kind of thing... I guess the only difference between C and C++ is the way they handle output and input. C++ has cout and cin (which I find simpler than scanf and printf in C). So why try to change the code when only the Input Output is different...

If that's the only difference, why use C++ at all? Obviously you don't really know the difference between them. It's much more complicated than input & output, which is only a minuscule difference.

This article has been dead for over six months. Start a new discussion instead.