How do I reverse a string?

This is a discussion on How do I reverse a string? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Is there a function in C that.... Haha. Just kidding I bet Govtcheez and Quzah are the first to jump ...

  1. #1
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892

    How do I reverse a string?

    Is there a function in C that....


    Haha. Just kidding
    I bet Govtcheez and Quzah are the first to jump all over this thread. Okay, it wasn't that funny...

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Judo CHOP!

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i feel so relieved...
    for a second i was readying my flame-thrower...

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    I do it like this

    siht ekil ti od I

    >>Judo CHOP!

    CHOP SALAD! Take that.
    Blue

  5. #5
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    quick and snappy sample:
    Code:
    unsigned char original_string[10], reversed_string[10];
    // both arrays should be same size
    for(int x=0;x<10;x++)
    {
     reversed_string[x] = original_string[10-x];
    }
    // note: u can also use sizeof for the original array
    think only with code.
    write only with source.

  6. #6
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    wait, a std function to reverse a string?
    maybe in string.h but I doubt it since I never use it.
    check the FAQs board

    also, if you want to save memory, change the reverse_string to just an unsigned char and swap values in the array instead.

    p.s.: thanks for your reply in the othello thread.
    Last edited by toaster; 07-18-2002 at 05:06 PM.
    think only with code.
    write only with source.

  7. #7
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Before you start being all "helpful" (bleh) maybe you should read the thread first?

  8. #8
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    helpful? me? hah. I seem more like a betrayer than a helper.

    I just read the first few lines in posts. I guess I deserved that.
    think only with code.
    write only with source.

  9. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Hehe, that was funny. I was like, "what's that guy doing?"

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    BTW Toaster, your code indexes outside the array when x is 0
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  11. #11
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    oops, I'll check it out.

    I suppose you know the error already, right?

    " reversed_string[x] = original_string[10-x]; "

    lol, how could I have not noticed that?

    thanks for the comment.

    quick correction: x<10; -> x<(10-1);
    Last edited by toaster; 08-05-2002 at 10:49 PM.
    think only with code.
    write only with source.

  12. #12
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Use strrev from string.h. If your compiler impementation doesn't support it ( it's not ANSI), write it yourself
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  13. #13
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    /me smacks CShot...

    bad yoda, bad. You're lucky I wasn't on here long last nite... i woulda been all over your cheez-attractin ass
    EntropySink. You know you have to click it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21