Theme Graphic
Theme Graphic

LaniSoft Corner

Programming with a view to maintenance. Code quality and common mistakes
Posted on Thursday, September 10, 2009 at 3:23 PM

Part 2 of How to Reverse the Words in a String (using C)

In the first part I showed how to reverse the words in a string with a simple Perl script. Perl is an excellent language for string manipulation; however, the original question was how could I write the algorithm using C# or C++. Take the following simple string:
A stitch in time saves nine
One way to reverse the words would be to take parse the string into individual words. These words could be stored in individual strings or they could be pushed onto a stack. For example (using _ to indicate the pointer position in the string for each operation and using {} to indicate stack elements with as {top...bottom}):

Before parsing:         _A stitch in time saves nine         stack = {}
1st Read:                      A_stitch in time saves nine        stack={A}
2nd Read:                     A stitch_in time saves nine        stack={stitch A}...
Comments: 1 Tags: C algorithm

 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.