How to Reverse Words within a sentence in C++

This is yet another popular interview question: given a string, for example, “Ask, and you might get it.”, write a C/C++ function to reverse the words so that it becomes: “it. get might you and Ask,”. Since I have documented my source code fairly extensively, you can read the source code and see how to do it: ReverseWords.txt

Update: I have tested ReverseWords.txt (after renaming it to .cpp) under Mac OS X 10.4.11 and verified that it worked. However, some one has reported crashing when building and running under Visual Studio 2005. For that, I submitted my vs2005 project and hope that the same person can help me to verify the correctness of my code. I appreciate your help. Click here for the vs2005 project.


13 thoughts on “How to Reverse Words within a sentence in C++

  1. wuhrr Post author

    I apology, when I converted the code to HTML for display, the null char got munched, and the quote chars got screwed up. This time, I uploaded the file as a pure text file, just change the extension to .cpp, build and run it. Thank you for reading my post.

  2. rupeshmandke

    hey still not working brother 🙂
    i tried running the application in VS2005 , it crashes!

    i tried copy pasting ur logic into mine & i see no output at all but its not crashing in my application.

    sorry, but am sort of absolute noob 🙂

  3. wuhrr Post author

    Hmm… that is funny, I have tested last night under OS X 10.4.11 and it worked. Today, I created a visual studio 2005 project and tested it and it still work. So Rupesh, would you please download my VS2005 project and see if it works? I appreciate it. You can find it here.

  4. rupeshmandke

    finally got the working version 🙂

    hey btw, this question is troubling me a lot, am not able to debug any of my C++ code in VS2005.

    i put breakpoints, but it never encounters them.

    the msg says : The breakpoint will not currently be hit. No symbiols have loaded for this document.

    whats this all about, do i have to set any specific project properties??

    sounds absolute noob questions, but dats wht i am 😀

    thanks for helping me sooo much, i really appriciate it 🙂


  5. rupeshmandke

    This solved a problem for me 🙂

    1) Goto Project->Properties
    2) Make sure “Configuration” at the top is “Debug”
    3) On the left, select “C/C++”, then “General”
    4) On the right, change “Debug information format” to “Program Database for edit and continue (/ZI)”
    5) On the left, Select “Optimization”
    6) On the right, Change “Optimization” to “Disabled (/Od)”
    7) On the left, select “Code Generation”
    8) On the right, change “Runtime library” to “Multi-Threaded Debug (/MTd)”
    9) On the left, expand “Linker” and select “Debugging”
    10) On the right, change “Generate Debug info” to “Yes (/DEBUG)”
    11) Rebuild your project.

  6. Hai Post author

    @chinmaya: The code is accessible: please click on the link to ReverseWords.txt at the end of the first paragraph. I appreciate you reading my blog.

  7. DJ

    The reason why it doesn’t work for some people is because of the first line of main().

    You wrote:
    char * sample = “He loves to eat apples.”;
    It should be:
    char sample[] = “He loves to eat apples.”;

    This is because if you don’t allocate an array of chars on the stack, but just declare a pointer, sample will be pointing to a CONST literal string in read-only memory, so attempts to modify will fail depending on your compiler and its settings.

    (C++ standard says that operation is undefined and I say setting a literal string to a non-const char* (without copying it to the stack or heap) is bad coding practices)

    Hope this helps 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s