Hai’s Blog

November 9, 2007

How to Reverse Words within a sentence in C++

Filed under: Interview, Programming — Hai @ 6:19 pm

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.

9 Comments »

  1. not working !!!

    Comment by rupeshmandke — November 20, 2007 @ 11:28 am

  2. 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.
    Hai

    Comment by wuhrr — November 21, 2007 @ 7:02 am

  3. 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 :)

    Comment by rupeshmandke — November 21, 2007 @ 7:28 am

  4. 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.

    Comment by wuhrr — November 21, 2007 @ 5:03 pm

  5. yup!
    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 :D

    thanks for helping me sooo much, i really appriciate it :)

    cheers!

    Comment by rupeshmandke — November 22, 2007 @ 5:54 am

  6. I did not have this problem, but here are some suggestions: do a build/clean, then rebuild. If it does not work, try the suggestions from here, and here, and here. If all else failed, google it. Please let me know which solution works.

    Comment by wuhrr — November 22, 2007 @ 10:00 am

  7. 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.

    Comment by rupeshmandke — November 23, 2007 @ 7:10 am

  8. your vc2005 code is not accessible please mail it or give a link to it.

    Comment by chinmaya — July 6, 2009 @ 2:31 pm

  9. @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.

    Comment by Hai — July 6, 2009 @ 3:49 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.