-
Test Palindrome
HI,
My goal is to write a program to test if a particular string is a palindrome (string that's spelled the same way backwards and forwards ). Here is the code but it doesn't work, it always return false, i don'tk now why, pls tell me whats wrong, thnx in advance!
Code:
/* Palindrome */
#include <stdio.h>
int testPalindrome( char array[] );
int main()
{
char string[ 6 ] = "ellle";
if ( testPalindrome( string ) == 1 )
printf( "String is a palindrome.\n " );
else if ( testPalindrome( string ) == 0 )
printf( "String is not a palindrome.\n" );
getch();
return 0;
}
int testPalindrome( char array[] )
{
int length = 0;
int i = 0;
i = 0;
while ( array[ i ] != '\0' ) {
++length;
i++;
}
if ( length - 1 == 0 || length - 1 == 1 )
return 1;
else if ( array[ 0 ] != array[ length - 2 ] )
return 0;
else {
array[ 0 ] = '\0';
array[ length - 2 ] = '\0';
for ( i = 0; i < 6; i++ )
printf( "%c", array[ i ] );
return testPalindrome( array );
}
}
-
You could write it
Code:
int palindrome(char* s)
{
return palindrome_recur(s, 0, strlen(s)-1);
}
int palindrome_recur(char* s, size_t low, size_t high)
{
if (low >= high)
return 1;
return s[low] == s[high] && palindrome_recur(s, low+1, high-1);
}
-
is there a way of doin it without using pointers ?
-
use strrev() to reverse the string and then test with strcmp() to see if the reversed string matches the original.
Should be about three lines.
EDIT
When I looked up strrev() in MSDN it has the code to do test for palindromes.
Code:
void main( void )
{
char string[100];
int result;
printf( "Input a string and I will tell you if it is a palindrome:\n" );
gets( string );
/* Reverse string and compare (ignore case): */
result = _stricmp( string, _strrev( _strdup( string ) ) );
if( result == 0 )
printf( "The string \"%s\" is a palindrome\n\n", string );
else
printf( "The string \"%s\" is not a palindrome\n\n", string );
}
-
the thing is, i am doin a exercise, which i can only use normal coding techniques. I can't use pointers coz i haven't learnt that yet and can't use any other functions except the stdio.h and the functoins that i wrote.
thnx
-
OK, should have realised, no one needs to find palindromes on their own.
Need a pointer to send the string into the function as an array and sending in the first element of the array (eg szBuffer[0]).
Could do it all in the main function or make the string global to avoid passing the pointer.
Code:
int FindPalindrome(char *szBuffer)
{
int iStart=0,iEnd=0;
while(szBuffer[iEnd] != '\0')
iEnd++;
//back off one so szBuffer[iEnd] is not = '\0'
iEnd--;
while(iStart<iEnd)
{
if(iStart==iEnd)//meet in the middle
return TRUE;
if( (szBuffer[iStart]) == (szBuffer[iEnd]) )
{
iStart++;
iEnd--;
}
else return FALSE;//not matched so not a palindrome
}
return TRUE;
}
call this with
if( FindPalindrome(szStringToTest) )
{
// is palindrome
}
else //is not
-
do u mean that it's not possible to do it recursively without the use of pointers ?
BTW your code worked beautifully thnx
-
It's possible, just replace
int FindPalindrome(char *szBuffer)
with
int FindPalindrome(char szBuffer[])
When you use array notation to pass a string to a function the compiler will turn it into a pointer, so both of the above calls will do the same thing.