/*String Reverse Author: Raymond Le Grand Description: Despite the instructor's abundant praise of graduate school [1], and his admonishment of the evils and terror of being a microserf, you have decided to follow Mr. Anderson [2] down the rabbit hole to the dark side; you are interviewing with the Redmond Beast. Of course, the vile dark lords of the evil empire are quite selective of their enslaved brood and have devised fiendish interrogation puzzles; cruel twisted feats of mental prowess. These programming tortures are meant to weed out the weakest of the candidates so that the Borg has the pleasure of assimilating the souls of only the strongest and 1337. The first of these devious interrogation problems is to reverse the words in a string in-place!!! Using pointers!!! [3] Notes: Only commas in middle of sentence, but end of sentence can contain other punctuation. You should only need 2 pointers and 1 temp character. */ #include #include #define MAX 256 using namespace std; void flipstring(char *begin, char *end){ char temp; //make sure the middle has not been reached while(true){ temp=*begin; *begin=*end; *end=temp; if ((begin==end)||(begin+1==end))break; begin++; end--; } } int main(){ char input[MAX]; while(true){ cin.getline(input,MAX); if (cin.eof()&&(*input=='\0'))break; //reverse entire line char *front; char *back; int punctuation; front=&input[0]; input[strlen(input)]='\0'; punctuation=1; back=&input[strlen(input)-punctuation-1]; flipstring(front,back); //reverse each word,starting with 1st word back=front; while(true){ //find next space while ((*back!='\0')&&(*back!=' '))back++; if (*back=='\0')punctuation++; if (*front==','){ flipstring(front+1,back-punctuation); *(front-1)=','; *front=' '; }else{ flipstring(front,back-punctuation); } if (*back=='\0') break; back++; front=back; } cout<