| | |
Interview Challenge (Word Reversal)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Let's pretend that I'm interviewing you for a C++ job. I want to know how you[1] approach problems and the extent of your C++ prowess, so I'm going to ask you to solve short problems in C++[2]. These problems range from beginner to advanced, and may be more than they seem. Good luck!
[1] "You" being collective. Everyone is welcome to put their heads together and/or build on the questions or solutions of everyone else. In fact, I encourage it.
[2] Each problem will have a separate thread, to keep things on-topic. If it turns out that this is a fun game, I'll post new problems regularly.
Okay then, here we go:
Problem: Write some code that reverses words.
[1] "You" being collective. Everyone is welcome to put their heads together and/or build on the questions or solutions of everyone else. In fact, I encourage it.
[2] Each problem will have a separate thread, to keep things on-topic. If it turns out that this is a fun game, I'll post new problems regularly.
Okay then, here we go:
Problem: Write some code that reverses words.
I'm here to prove you wrong.
My first thought which springs to mind, is, how would you define a 'word'?
is it..
My next thought... what do you mean by "reverses words"
Those questions aside, here's how i'd do it in C++ (Making assumptions about the above, of course
)
is it..
- A sequence of alphanumeric characters
- A sequence of alphanumeric characters & punctuation
- A sequence of purely Alphabetical characters
- Assuming any particular character set (ASCII, Unicode, etc)
My next thought... what do you mean by "reverses words"
- Reverses the order of a list of "words" (assuming "word" is defined)
- Reversing the order a set of characters appear in a word
- A combination of the above (Which would be more like reversing a 'sentence')
Those questions aside, here's how i'd do it in C++ (Making assumptions about the above, of course
) CPP Syntax (Toggle Plain Text)
#include <iostream> #include <ostream> #include <string> #include <sstream> #include <deque> #include <algorithm> #include <functional> class reverse_word : public std::unary_function<std::string, std::string> { public: std::string& operator()(std::string& str) { std::reverse(str.begin(), str.end()); return str; } }; int main() { std::string str ("the quick brown fox jumps over the lazy dog"); std::cout << str << std::endl; std::stringstream ss (str); typedef std::deque<std::string> list_t; list_t word_list; std::string temp; while( ss >> temp ) word_list.push_back(temp); std::for_each(word_list.begin(), word_list.end(), reverse_word() ); list_t::const_iterator i; for( i = word_list.begin() ; i != word_list.end() ; ++i ) std::cout << *i << " "; }
Last edited by Bench; Jul 13th, 2007 at 7:08 pm.
¿umop apisdn upside down? >My first thought which springs to mind, is, how would you define a 'word'?
That's an excellent reaction. After a moment's thought, I'm thinking we (the imaginary company you're interviewing for) could use something like this to process subroutine parameters in a compiler while building a stack frame. The parameters are evaluated from last to first, but written first to last. So for our needs a word is a valid identifier (our compiler uses the same identifier rules as C++).
>My next thought... what do you mean by "reverses words"
Another good question. We'll be using the parameter identifiers themselves as keys into a map for the symbol table, so it's probably not a good idea to modify the source strings. So in our case, the list
>Those questions aside, here's how i'd do it in C++
I like it. But I forgot to tell you that our development environment is extremely buggy and resource intensive (management is looking into replacing it). Using the standard library would be a bad idea for this component since it'll be used very often during a compilation phase.
That's an excellent reaction. After a moment's thought, I'm thinking we (the imaginary company you're interviewing for) could use something like this to process subroutine parameters in a compiler while building a stack frame. The parameters are evaluated from last to first, but written first to last. So for our needs a word is a valid identifier (our compiler uses the same identifier rules as C++).
>My next thought... what do you mean by "reverses words"
Another good question. We'll be using the parameter identifiers themselves as keys into a map for the symbol table, so it's probably not a good idea to modify the source strings. So in our case, the list
[arg1, arg2, arg3] would be reversed into [arg3, arg2, arg1].>Those questions aside, here's how i'd do it in C++
I like it. But I forgot to tell you that our development environment is extremely buggy and resource intensive (management is looking into replacing it). Using the standard library would be a bad idea for this component since it'll be used very often during a compilation phase.
I'm here to prove you wrong.
simple and it works 
i know of a few things i could do to optimize but for the question it works...or so i believe

C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <string> #include <cstdlib> using namespace std; void reverseword(char * reverse) { size_t size = strlen(reverse); //get the size of the char * for(size_t i = 1; i <= size; i++) { cout << reverse[size - i]; //output the word } } void reverseword (string reverse) { size_t size = reverse.size(); string::reverse_iterator walkword; for(walkword = reverse.rbegin(); walkword < reverse.rend(); walkword++) { cout << *walkword; } } int _tmain(int argc, _TCHAR* argv[]) { cout << "Method one for string reversal" << endl; cout << "==============================" << endl; cout << "== Using Char * and cin.getline" << endl; cout << "== using for statement to itterate" << endl; cout << "==============================" << endl; char * getwords = new char(); //will hold the words cout << "Please enter the words to reverse" << endl << endl << endl; cin.getline(getwords,100); //get the word(s) to reverse reverseword(getwords); //reverse the words cin.sync();//clean up buffer for next example cout << endl << endl << endl; cout << "Method Two for string reversal" << endl; cout << "==============================" << endl; cout << "== Using std::basic_string cin.getline" << endl; cout << "== using build in itterator members" << endl; cout << "==============================" << endl; string getwordsagain; cout << "Please enter the words to reverse" << endl << endl << endl; getline(cin, getwordsagain); reverseword(getwordsagain); cin.sync(); //clean the buffah return 0; }
i know of a few things i could do to optimize but for the question it works...or so i believe
Dont forget to spread the reputation to those that deserve!
>simple and it works
Your code exhibits undefined and implementation-defined behavior (start a new thread asking why if you want details). It also alters the words, which won't work for us, we need the words to be reversed without also reversing the characters in each word. But it's a start.
Your code exhibits undefined and implementation-defined behavior (start a new thread asking why if you want details). It also alters the words, which won't work for us, we need the words to be reversed without also reversing the characters in each word. But it's a start.
I'm here to prove you wrong.
•
•
•
•
>simple and it works
Your code exhibits undefined and implementation-defined behavior (start a new thread asking why if you want details). It also alters the words, which won't work for us, we need the words to be reversed without also reversing the characters in each word. But it's a start.
so from what i am gathering if i were to input the string
hello world
you dont want
dlrow olleh
you want
olleh dlrow
the words reversed but their location is intact?

i figure i should ask questions now rather than later
Dont forget to spread the reputation to those that deserve!
![]() |
Other Threads in the C++ Forum
- Previous Thread: ordered Linked List Copy function
- Next Thread: the middle character of a string
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






