//Ian Gavlick //Problem 007 //String Word Reversal #include #include using namespace std; void reverse(int begin, int end, char str[]){ char tmp; while(end>begin){ tmp = str[begin]; str[begin] = str[end]; str[end] = tmp; begin++; end--; } } int main() { while(true){//For each line char line[200]; if(cin.getline(line,200).eof()) break; int s = 0; int e = strlen(line)-2; reverse(s,e,line);//Reverse entire string, except for end punctuation. e = 0; bool done = false; while(true){//For each word while(line[e] != ' '){//Parse through each character until the space e++; if(e > strlen(line)-2){ done = true; break; } } e--; reverse(s,e,line); if(done) break; e+=2; s=e; if(line[e]==','){//If there's a , at the start of a word, move it to the end of the previous. line[e-1]=','; line[e]=' '; } } cout << line << endl; } return 0; }