Skip to main content
GameDev.net gamedev.net
🔒 Locked

Searching Strings

Started by MrPoopypants Feb 19, 2003 at 6:22 PM 13 replies 450+ views
Original Post
MrPoopypants
MrPoopypants
Hi guys... My little project here is becomming very compliacted Im trying to find out if a string is a palindrome by using pointers. This code here compiles without an error but crashes whenever i run it
	char e, e2;
	char *ePtr, *e2Ptr;

	e = writePtr[ 0 ];
	ePtr = &e
	e2 = writePtr[ numberofchars ];
	e2Ptr = &e2

	if ( numberofchars % 2 == 0)
	{
		//number is even so check first and last moving inwards
		for ( int a = 0; a < numberofchars / 2; ePtr++, e2Ptr-- )
		{
			if ( *ePtr == *e2Ptr)
			{
                                //we gotta match!
				numberofpals++;
			}
		}
		if ( numberofpals == ( numberofchars / 2) )
		{
			cout << "Palindrome" << endl;
			return true;
		}

	}
 
Any assistance would be greatly appreciated Thanks a lot.
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
fizban75
fizban75
Ugh, typical CS coursework. Is this a homework assignment? Because I''m not gonna do your schoolwork for you.

e2 = writePtr[ numberofchars ];

All I''m going to do is tell you that this statement is your problem.

Secondly, you don''t need e and e2. Get rid of them.
MrPoopypants
MrPoopypants
Well thanks a lot but can you possibly be more specific.

Ive edited the code a lot but this still generates no error messages upon compilation but crashes whenever i run it:


    
bool TestForPalindrome ( char * P )
{
int numberofchars = 0, numberofpals = 0;
char writeStr [ MAXLEN ]= "";
char * writePtr = writeStr;
char * readPtr = P;

//Check for commas, apostrophies, periods, and spaces

//If one is found, stop the writePtr from moving forward

//To skip invalid characters.

while ( *readPtr != '\0' )
{

if ( *readPtr == '\'' || *readPtr == ',' || *readPtr == '.' || *readPtr == ' ' )
{
readPtr++;
}
else
{
*writePtr = *readPtr;
if ( islower(*readPtr) )
{
*writePtr = toupper( *writePtr);
}
readPtr++;
writePtr++;
numberofchars++;
}
}
//for test

cout << "
write = " << writeStr << endl;

//Now check if string is palindrome

//this is wher things get hairy

char *ePtr,
*e2Ptr;

ePtr = &writeStr[ 0 ];
e2Ptr = &writeStr[ numberofchars - 1 ];

if ( numberofchars % 2 == 0)
{
//number is even so check first and last moving inwards

for ( int a = 0; a <= numberofchars / 2; ePtr++, e2Ptr-- )
{
if ( *ePtr == *e2Ptr)
{
numberofpals++;
}
}
if ( numberofpals == numberofchars / 2 )
{
cout << "
It's a palindrome!" << endl;
return true;
}

}
else
{
//number is odd so do same as above, just leave out middle char

}

return false;
}


Again, if anyone could help me out here that would be great

Thanks

[edited by - MrPoopypants on February 19, 2003 8:38:59 PM]
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
Lunatic Raven
Lunatic Raven
Just out of curiousity, why are you using pointers? You could easily solve this problem by using the pseudo-code below, which I think is much simplier.


bool TestForPalindrome (char *string)
{
char compare_string [1024]; // This is assuming the user wouldn't enter anything larger than that
int length_string = strlen(string);

for (int t = 0; t < length_string; t++)
{
compare_string[t] = string[length_string-t];
}

if (strcmp(compare_string, string) == 0)
{
return true;
}

return false;
}


I believe that should work, but then again, their may be some mistakes, cuz I wrote that up fast and didn't test it, but the general idea is there.

[edited by - Lunatic Raven on February 19, 2003 8:49:38 PM]
"...."
MrPoopypants
MrPoopypants
quote:

Just out of curiousity, why are you using pointers?


well.. because i have to

Anyway Lunatic Raven thanks a lot for trying to help out.
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
fizban75
fizban75
Like I asked before, is this a homework assignment? Because if it is, I''m not helping you. I know what''s wrong, but if this is classwork, then it''s for you to figure out, not for me to tell you the answer.

But, since I''m a nice guy, here''s a clue. Look closely at your for loop. You''re forgetting something.
TerranFury
TerranFury
There''s an even easier way I came up with, and it''s a heck of a lot more efficient; you can check if it''s a palindrome without copying it to another string.

Simply (and this is common sense if you think about it), a string is a palindrome if...

...the first letter is the same as the last
...the second letter is the same as the second to last
...the third letter is the same as the third to last
...and so on.

Pretty simple, eh?

You can do this with pointers easily. Create one character pointer to the beginning of the string, and another to the end. Each loop, increment the bottom one and decrement the top one until they''re next to each other or equal.

If I gave you any more you wouldn''t have any assignment left to do on your own, so I''ll leave it at that.
MrPoopypants
MrPoopypants
I actually got it figured out in a MUCH simpler way:


bool TestForPalindrome ( char * const a )
{
int numberofchars = 0;
char writeStr [ MAXLEN ]= "";
char * writePtr = writeStr;
char * readPtr = a;

/* Check for commas, apostrophies, periods, and spaces
* If one is found, stop the writePtr from moving forward
* To overwrite the invalid character
*/
while ( *readPtr != '\0' )
{

if ( *readPtr == '\'' || *readPtr == ',' || *readPtr == '.' || *readPtr == ' ' )
{
readPtr++;
}
else
{
*writePtr = *readPtr;
if ( islower(*readPtr) )
{ //convert to upper case if not already
*writePtr = toupper( *writePtr);
}
readPtr++;
writePtr++;
numberofchars++;
}
}

//for test
// cout << "write = " << writeStr << endl;

//Check if Palindrome
char *ptrToFront;
char *ptrToBack;

ptrToFront = writeStr;
ptrToBack = writeStr + strlen ( writeStr ) -1 ;
while ( ptrToBack >= ptrToFront )
{
if ( *ptrToFront != *ptrToBack )
{ //Is not a palindrome
return false;
}
ptrToFront++;
ptrToBack--;
}
//Is a palindrome
return true;
}


Thanks to you all who helped out though.

[edited by - MrPoopypants on February 19, 2003 12:56:12 AM]
(0110101101000110)The Murphy Philosophy: Smile . . . tomorrow will be worse.
civguy
civguy
you don't have to copy the string..(hum, I think giving this now is okay since he solved the assignment already)
  
bool testForPalindrome(const char* text) {
const char* pFront = text;
const char* pBack = text + strlen(text) - 1;
while (pBack >= pFront) {
if (!isalpha(*pFront))
pFront++;
else if (!isalpha(*pBack))
pBack--;
else {
if (toupper(*pFront) != toupper(*pBack))
return false;
pFront++;
pBack--;
}
}
return true;
}


[edited by - civguy on February 20, 2003 2:47:07 AM]
fizban75
fizban75
PoopyPants, since you''ve found another solution, I''ll tell you what was wrong with the 2nd iteration of your code.

You weren''t increasing the value of ''a'' in your for loop, so it looped until the pointers went out of bounds and then they caused memory access errors.
YoshiN
YoshiN

  
bool isPalindrone(char *text, int beg, int end)
{
if(text[beg] == text[end])
return(isPalindrone(text, beg+1, end-1));
else
return(false);
}
-YoshiXGXCX ''99
Alan Kemp
Alan Kemp
STL is your friend (ok, so not when its an assignment that has to be done with pointers...)


  
bool IsPalindrome(const char * str)
{
std::string str2 = str;
std::reverse(str2.begin(), str2.end());
return (str == str2);
}


Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
civguy
civguy
STL is indeed fun to toy with. Here's a version that's case insensitive and ignores non-alphabetic letters:

bool isPalindrome(string text) {
text.erase( remove_if(text.begin(), text.end(), not1(ptr_fun(isalpha))) , text.end());
transform(text.begin(), text.end(), text.begin(), toupper);
return equal(text.begin(), text.end(), text.rbegin());
}


[edited by - civguy on February 21, 2003 7:49:35 AM]
Pipo DeClown
Pipo DeClown
void CompareText( char* _String1, char* _String2, long _Length )
{
long Counter = 0;
long Size = Length;
char Compare1[Size] = ;
char Compare2[Size] = ;

strcpy( Compare1, _String1 );
strcpy( Compare2, _String2 );

for( Counter; Counter < Size; Counter++ )
{
if( Compare1[Counter] != Compare2[Counter] )
return FALSE;
}

return TRUE;
}

strcmp didnt work for me, i dont know why, so i made this function.

.lick

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.