Is there any way to reverse a string (I am trying to test for a palindrome)? Anything like string::reverse()?
Printable View
Is there any way to reverse a string (I am trying to test for a palindrome)? Anything like string::reverse()?
I think there's a strrev function...give me a sec and I'll check on msdn for you :)
Thanks :)
Not sure if this'll compile, but it's a start...I'm sure you can tweak that for speed using copy and some interesting iterator code, but that might work :)Code:string str = "Palindrome";
string revstr;
for(reverse_iterator iter = str.rbegin(); iter != str.rend(); ++iter) {
revstr += *iter;
}
Ehh, I just needed something simple for my computer class. I did this basically:PHP Code:string input = "racecar";
string revstr = input;
strrev(revstr.c_str());
if (input == revstr)
cout << input << " is a palindrome." << endl;
Don't think you can do that. c_str() returns a const char*, so you can't change it...
test this:
take a look at the link steve posted.....Code:#include <iostream>
#include <string>
using namespace std;
int main()
{
char string[20] = "racecar";
char string_orig[21];
for(int i=0;i<22;i++)
string_orig[i] = string[i];
if(strcmp(string, strrev(string_orig)) == 0)
cout << string << " is a palindrom " << endl;
else
cout << string << " is not a palindrom " << endl;
return 0;
}
hope this helps!
*wags finger*Code:char string[20] = "racecar";
Bad boy, hiding library names like that ;) You're not allowed to use string as an identifier name since it's a class name.
Well, you CAN, but you SHOULDN'T :)
Parksie, I think you shouldn't make up your own list of keywords. It's perfectly legal to use any names (that are not c++ keywords) as local variables, then if you want to access string you use the global scope operator ::Quote:
Originally posted by parksie
*wags finger*Code:char string[20] = "racecar";
Bad boy, hiding library names like that ;) You're not allowed to use string as an identifier name since it's a class name.
Well, you CAN, but you SHOULDN'T :)
But the names in the standard library are as close as it gets to keywords.
Plus, you shouldn't hide names from an outer block scope when you're still in the same namespace.
And especially not the standard names - it'll cause great confusion later.
PS: It's not my list of keywords, it's ISO's :p
bullcrap!Quote:
Plus, you shouldn't hide names from an outer block scope when you're still in the same namespace.
It's perfectly legable and doesn't hide anything
Here's all what you can do with scope operators to access member, base member, global and local variables:
PHP Code:int parsi=1;
class bl{
protected:
int parsi;
};
class bla: public bl{
int parsi;
bla(){
int parsi=2;
cout<<parsi//local
cout<<:: parsi;//global
cout<<bla:: parsi;//member
cout<<bl:: parsi;//base
};
};
I understand what you mean parksie, but for Wynd's computer class assignment I think it's ok for him to use the words, that I used.
But I'll think about this in the future.......:)
I don't know if this is "good" programming, but it works. I did thisand it works fine.PHP Code:strrev((char *)output.c_str());
I thought strrev changed the string you passed to it.
You can cast the const-ness of the .c_str() using const_cast, but you're not supposed to (what if it's in ROM?). In this case you can get away with it because it doesn't change the length.