|
-
Dec 15th, 2001, 12:35 PM
#1
Thread Starter
Fanatic Member
Reversing a string
Is there any way to reverse a string (I am trying to test for a palindrome)? Anything like string::reverse()?
Alcohol & calculus don't mix.
Never drink & derive.
-
Dec 15th, 2001, 01:10 PM
#2
Frenzied Member
I think there's a strrev function...give me a sec and I'll check on msdn for you
-
Dec 15th, 2001, 01:14 PM
#3
Frenzied Member
-
Dec 15th, 2001, 01:30 PM
#4
Thread Starter
Fanatic Member
Thanks
Alcohol & calculus don't mix.
Never drink & derive.
-
Dec 15th, 2001, 03:45 PM
#5
Monday Morning Lunatic
Not sure if this'll compile, but it's a start...
Code:
string str = "Palindrome";
string revstr;
for(reverse_iterator iter = str.rbegin(); iter != str.rend(); ++iter) {
revstr += *iter;
}
I'm sure you can tweak that for speed using copy and some interesting iterator code, but that might work
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 15th, 2001, 04:02 PM
#6
Thread Starter
Fanatic Member
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;
Alcohol & calculus don't mix.
Never drink & derive.
-
Dec 15th, 2001, 04:05 PM
#7
Monday Morning Lunatic
Don't think you can do that. c_str() returns a const char*, so you can't change it...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 16th, 2001, 06:06 AM
#8
Addicted Member
test this:
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;
}
take a look at the link steve posted.....
hope this helps!
-
Dec 16th, 2001, 06:28 AM
#9
Monday Morning Lunatic
Code:
char string[20] = "racecar";
*wags finger*
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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 16th, 2001, 06:37 AM
#10
transcendental analytic
Originally posted by parksie
Code:
char string[20] = "racecar";
*wags finger*
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 ::
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 16th, 2001, 06:39 AM
#11
Monday Morning Lunatic
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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 16th, 2001, 06:57 AM
#12
transcendental analytic
Plus, you shouldn't hide names from an outer block scope when you're still in the same namespace.
bullcrap!
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
};
};
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 16th, 2001, 08:59 AM
#13
Addicted Member
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.......
-
Dec 16th, 2001, 12:56 PM
#14
Thread Starter
Fanatic Member
I don't know if this is "good" programming, but it works. I did this
PHP Code:
strrev((char *)output.c_str());
and it works fine.
Alcohol & calculus don't mix.
Never drink & derive.
-
Dec 16th, 2001, 01:49 PM
#15
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|