Reverse a string c/c++

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

This question got thrown at me today.

Reverse a string without using pointers or arrays (Including cstrings).

Im still wrapping my head around this one and wanted to throw it out here to see if anyone else has an idea.

The more I think about this, the more I think they question was asked incorrectly. Even if you wanted to get the string directly from the user and write an overloaded `istream >>` operator Im not sure how to deal with the input without using an array of some sort or pointers (using a linked list).


I guess if you used a std list you wouldn't be using any pointers if you didn't declare any in your program.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I got it!

Kind of sneaky, really quite simple though. Anyone hazard a guess?
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

C-strings are already arrays, and any time you're working with arrays, you're working (directly or indirectly) with pointers.

Did I miss something? :scratchhead:
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I've come to the conclusion that your just not allowed to declare any pointers yourself. Which brings in the use of std lists, stacks.

The solution I came up with doesn't use any pointers as far as I can tell besides maybe some deep underlying ones. But my solution also doesn't really "reverse" a "string" It takes an input from the user and prints it backwards using recursion. As I don't have any way of storing the input without some sort of data structure.

The real question was worded "Reverse a string without using pointers", but I added the array/cstrings so people didn't just declare a string and traverse it backwards. As most people would neglect the facts you stated.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

This is why you always want to ask clarification questions before jumping in and trying to answer the question. If someone had presented me with that in an interview, immediately two things come to mind:

1) What state am I starting in, or, how is the string presented to me initially (in memory, in a stream, etc).

2) How does it need to be reversed? In memory? As output only?

Interviewers often leave questions vague like this because they want to see your thought process; they want you to ask questions because how you approach the problem often tells a lot more about your problem solving skills then the actual code on the whiteboard.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 98 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.