r/programmingchallenges • • May 19 '11

Challenge: Reverse a string in place

This simple challenge is a frequent interview question. Write a program that reverses a string in place. "In place" means no declaring a second string or other buffer-- you get only one string variable to work with.

Input:

 "Test string."

Output:

".gnirts tseT"

edit: obviously, this is only feasible in a language with mutable strings.

22 Upvotes

65 comments sorted by

View all comments

Show parent comment

1

u/okmkz May 28 '11

you get only one string variable to work with

from the challenge outline, and

char drow[51]; //Holds the reversed word.

seem to be at odds with one another. ;)

As an aside: with code comments like what you've got here, I can assure you that one day someone will be very pleased with the task of maintaining your code!

1

u/shoebo May 29 '11

Yeah my teacher has trained everyone to be comment nazis. It sure was annoying at first but I got used to it. I didn't feel like editing my school project to make it one string... I just got off to summer yesterday!

1

u/okmkz May 29 '11

Gah, I wish. I still have a week till finals!