3 Replies - 729 Views - Last Post: 17 October 2012 - 03:14 PM Rate Topic: -----

#1 MrHatchi87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-October 12

Understanding Palindrome

Posted 17 October 2012 - 02:49 PM

Hey there, I appreciate you taking the time to click on my topic! Anyways I need help understanding some logic/code for an assignment.

I need to write 2 programs to determine if a string represents a palindrome. The first program
is based on string objects while the second is based on c-strings. I don't understand those at all. Can someone please point me in the right direction that can help me understand it? I also need to include this bit of code:

int x = s.find(' '); // see p. 307
while(x != -1 && x < (signed)s.size())
{
s.erase(x, 1);
x = s.find(' ');
}




not really sure where and or how to incorporate that into the code.

Again, I really appreciate the help

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding Palindrome

#2 jimblumberg   User is online

  • member icon

Reputation: 5489
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Understanding Palindrome

Posted 17 October 2012 - 02:52 PM

I don't know where to incorporate that into your code either, because you haven't posted your code. You need to show your code where you are trying to solve your problem, and then ask specific questions based on the code you post.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 408
  • View blog
  • Posts: 882
  • Joined: 27-June 09

Re: Understanding Palindrome

Posted 17 October 2012 - 03:07 PM

A c string is an array of characters that has a special character '\0' at the end of it. So if your string is "palindrome", then the corresponding c string would be {'p','a','l','i','n','d','r','o','m','e','\0'}. You can poke through these references for help.

C++ String
C++ C_String

The block of code you posted removes spaces from the string. s.find(' ') returns the location of the first space in the string or -1 if there are no spaces. So, the code keeps looping and erasing spaces until there are none left. You want to do this to the string before checking if it is a palindrome. Spaces rarely match in palindromes.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5489
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Understanding Palindrome

Posted 17 October 2012 - 03:14 PM

Actually the snippet he posted is incorrect because the std::string.find() doesn't return an int it returns a size_t. A size_t can never be a negative value, it is an unsigned type. Also note that when find() fails to find the value it returns std::string::npos which is a very large number (the largest value a size_t can hold), probably much larger than what can be held in an signed int. Also note that relying on signed int overflow is undefined behavior, which is what you get when you try to put npos into an int.

Jim

This post has been edited by jimblumberg: 17 October 2012 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1