View Full Version : How do I reverse a string?

07-18-2002, 01:55 PM
Is there a function in C that....

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

07-18-2002, 01:57 PM
Judo CHOP!

07-18-2002, 01:59 PM
i feel so relieved...
for a second i was readying my flame-thrower...

07-18-2002, 04:29 PM
I do it like this

siht ekil ti od I

>>Judo CHOP!

CHOP SALAD! Take that.

07-18-2002, 04:50 PM
quick and snappy sample:

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

07-18-2002, 05:04 PM
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.

07-18-2002, 05:23 PM
Before you start being all "helpful" (bleh) maybe you should read the thread first?

07-18-2002, 05:27 PM
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.

07-18-2002, 05:51 PM
Hehe, that was funny. I was like, "what's that guy doing?"

08-05-2002, 10:41 PM
BTW Toaster, your code indexes outside the array when x is 0

08-05-2002, 10:45 PM
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);

08-06-2002, 12:06 AM
Use strrev from string.h. If your compiler impementation doesn't support it ( it's not ANSI), write it yourself :p

08-06-2002, 05:35 AM
/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 ;)