I am trying to create a program to solve a problem in a textbook that I bought recently, and it just drives me crazy.
I need to create a reverse of the sentence, so I get the following:
Input = "To do or not, no attempt." Output = "try. No no, do or Do"
Here is what I have so far:
void ReverseString::reversalOperation(char str[]) { char* buffer; int stringReadPos, wordReadPos, writePos = 0; // Position of the last character is length -1 stringReadPos = strlen(str) - 1; buffer = new char[stringReadPos+1]; while (stringReadPos >= 0) { if (str[stringReadPos] == ' ') { wordReadPos = stringReadPos + 1; buffer[writePos++] = str[stringReadPos--]; while (str[wordReadPos] != ' ') { buffer[writePos] = str[wordReadPos]; writePos++; wordReadPos++; } } else { stringReadPos--; } } cout << str << endl; cout << buffer << endl; }
I was sure that I was on the right track, but all I get for the conclusion is the very first word ("try"). I have looked at this code for so long that I cannot succeed. Initially, I checked the inside looking for the '/ 0' character, but it didnβt seem to me, so I took it.
source share