Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 2nd, 2006, 8:12 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Testing for a palindrome using std::string

I wrote a simple program to test for a palindrome, and for some reason, a simple comparison of strings does not return good results. I have a feeling this has to do with comparing references, though I don't really know. As usual, I'm sure it is something stupid:

#include <string>
#include <iostream>

std::string reverse(const std::string &str);
bool palindrome(const std::string &str);

int main() {
    const std::string msg = "abba";
    if(palindrome(msg))
        std::cout << "Msg is a palindrome" << std::endl;
	
	return 0;
}

std::string reverse(const std::string &str) {
	std::string result;

	for(int x = str.size(); x > -1; x--)
		result += str[x];

	return result;
}

bool palindrome(const std::string &str) {
	return reverse(str) == str;
}

I am 99.99% sure that my reverse() function works correctly, but I still don't get good output. Any tips would be great. Thanks.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old May 2nd, 2006, 8:28 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
In your reverse function, start the for loop from std.size() - 1 like this:
for(int x = str.size()-1; x > -1; x--)
		result += str[x];
size() returns the actual size of the string, not the subscript of the last character.
OpenLoop is offline   Reply With Quote
Old May 2nd, 2006, 8:30 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Reverse returns a string. You've defined palindrome to return a bool, but are attempting to return a string. The existence of the string will cause a true to be returned. You should have mentioned HOW it was failing, not just that it was. A debugger or some debug output before and after each return would tell you a lot.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 2nd, 2006, 8:36 PM   #4
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by DaWei
You've defined palindrome to return a bool, but are attempting to return a string.
return reverse(str) == str;

What?
Jason Isom is offline   Reply With Quote
Old May 2nd, 2006, 8:37 PM   #5
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
But wouldn't palindrome return the return value of his comparison?
Twilight is offline   Reply With Quote
Old May 2nd, 2006, 8:47 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Why not just use something like:
for(int i = 0, j = str.length - 1; i < j; i++, j--)
   if(str[i] != str[j])
      return false;
return true;
That way you don't create a new string, and you only iterate over 1/2 of the string instead of the whole thing to reverse it...
Jimbo is offline   Reply With Quote
Old May 2nd, 2006, 8:58 PM   #7
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Jimbo: Because I hadn't thought of that :p

OpenLoop: Yup, that fixed it. Thanks

DaWei: You did misread the code, but I suppose it is partly my fault for not explaining the exact errors I was recieving (in this case, everything ran, but I didn't get the output I wanted.)
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old May 2nd, 2006, 9:23 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
My apologies, I did misread the code.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 3rd, 2006, 1:27 AM   #9
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 931
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I just had to post this because I saw it in a C++ book I was reading. It tests for a palindrome in a single line.

bool is_palindrome(const string& s)
{
    return equal(s.begin(), s.end(), s.rbegin());
}
titaniumdecoy is offline   Reply With Quote
Old May 3rd, 2006, 2:55 AM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Its nice and short, but a simple addition makes it take about half as long to run:
bool is_palindrome(const string& s)
{
    return equal(s.begin(), s.begin() + s.length() / 2, s.rbegin());
}
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC