0

Hey guys, I need a little help. I'm writing a palindrome checker for fun, and I need to ignore/delete spaces and punctuation. I compare my two strings using strcmp and I use pointers to go through the first part of the program. I'm now stuck trying to factor out the spaces and punctuation, and I'm guessing I will have to do something b4 the strcmp is called. What should I do to delete all the spaces and punctuation?

Thanks,
MacMan101

5
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by lotrsimp12345
0

Copy you string a character at a time to another string. But move only the alphabetic characters. See firstPerson's link

0

So how exactly would you implement that? Changing all the alphanumeric characters to a new string? And this is to test for palindromes, so I would just do it twice for the forward string and reverse string right?

0

Why not use the string class?

for(unsigned i=0; i<str.length(); i++){
		switch(str[i])
		{
		case ' ':
			str.erase(i, 1);
			break;
		default: break;
		}
	}

This way it's easy to remove custom characters. Of course, when copying the characters into another string/array it's simple to apply toupper() or tolower().
Or you could include the algorithm header and change the case with transform(str.begin(), str.end(), str.begin(), tolower).

0

So how exactly would you implement that?

By writing out a string and doing what I recommended on paper. Then understanding what you just did and writing code based on that understanding.

Changing all the alphanumeric characters to a new string?

Changing? Did I say changing?

And this is to test for palindromes, so I would just do it twice for the forward string and reverse string right?

Again, think about how you can tell a string is a palindrome. Write it out.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.