![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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]; |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
return reverse(str) == str; What? |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
But wouldn't palindrome return the return value of his comparison?
|
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
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; |
|
|
|
|
|
#7 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 |
|
Expert Programmer
|
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());
} |
|
|
|
|
|
#10 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
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());
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|