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

Recommended Answers

All 6 Replies

Take a look at cctype
header. This is very handy.

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

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?

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).

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.

don't erase create substring just incase you still need the string later on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.