#include <iostream>
#include <cstring>
using namespace std;
void reverse(char* sentence)
{
int index = strlen(sentence) - 1;
char last = '';
int hold = 0;
while ( index != 0){
while (sentence[index] != ' ')
index--;
hold = index; //keeps the index of whitespace
while (sentence[index] != last){
cout << sentence[index]; //printing till it either hits end character or whitespace.
index++;
}
last = sentence[hold]; //Keeps the whitespace
index = hold; //
}
}
int main()
{
char* sentence = new char[256];
cin.getline(sentence, 256);
reverse(sentence);
}
I want to reverse the orders of words in a sentence and you can see my attempt above.
Sample intput and output should be like this:
Howdy Mr. Mcfly?
Mcfly? Mr. Howdy
Where i get:
Howdy Mr. Mcfly?
Mcfly?
There are many similar questions around the internet but what i want is to find error in my own code.
See Question&Answers more detail:os