Results 1 to 15 of 15

Thread: Reversing a string

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    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.

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    I think there's a strrev function...give me a sec and I'll check on msdn for you

  3. #3

  4. #4

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Thanks
    Alcohol & calculus don't mix.
    Never drink & derive.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  6. #6

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    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!
    [p r a e t o r i a n]

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  13. #13
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    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.......
    [p r a e t o r i a n]

  14. #14

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width