|
-
Jan 11th, 2002, 01:44 AM
#1
Thread Starter
Hyperactive Member
testPalindrome function: doesn't work
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 );
}
}
-
Jan 11th, 2002, 03:09 AM
#2
Lively Member
the most obvious problem is that an array of type char is not the same as a variable of type string. I'm not going to give you the whole answer because it sounds like some sort of school assignment.
-
Jan 11th, 2002, 03:16 AM
#3
Thread Starter
Hyperactive Member
it's not school assignment, i am learning C by myself and this is from an exercise from the deitel book. And i need examples to study and learn. Pls post a solution for me to study.
thnx
-
Jan 11th, 2002, 05:43 AM
#4
Frenzied Member
I'm not exactly sure how your version is meant to work iflash, so I'll just give you what my solution would be.
Basically, you have two char pointers. One starts at the beginning of the string, the other starts at the end of the string. Then, you repeatedly check equality of the characters the two pointers are pointing at. If they differ, you don't have a palendrome. Otherwise, move both pointers one character closer to the middle of the string (ie the start pointer increases, the end pointer decreases) and check again. Keep going until your pointers are less than 1 characters apart, which would mean that both pointers are pointing at the same character or have passed each other; in either case you don't have to keep going, you can jusreturn with a true value because you'll know it's a palendrome.
So here's some rough code to do that:
Code:
int testPalindrome( char *szPhrase )
{
char *pcStart = szPhrase;
char *pcEnd = szPhrase;
// move pcEnd to the end of the string
while ( *(pcEnd + 1) != '\0' )
pcEnd++;
while ( pcEnd > pcStart )
{
if ( *pcStart != *pcEnd )
return(0);
pcStart++;
pcEnd--;
}
//if the program got this far without returning
//then we know it's a palendrome
return(1);
}
it might have a few errors since I haven't checked it.
Of course, that is an example using just builtin C code. It would be much easier to just make a copy of the string, reverse it and compare that with the original, all using the basic string functions.
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|