// Reverse words in a sentence version 1.0
#include <iostream>
#include <Windows.h>

using namespace std;
using std::cout;
using std::endl;

char *StrRev(char *src) {
	int i = strlen(src);
	char *temp = new char[i];
	int j = 0;

	while(i >=0){
		temp[j] = src[i-1];
		j++;
		i--;
	}
	j--;
	temp[j] = '\0';
	return temp;
}

string RevWords(string source){
	char *src = NULL;
	char *temp = NULL;
	string s0 = "";
	char Buffer[128];
	int i = 0;
	int j = 0;
	char c = '\0';

	src = new char[strlen(source.c_str())];
	//Copy string to src
	strcpy(src,source.c_str());
	//Reverse the string
	src = StrRev(src);

	//Below code scans the string looking for a space
	//When a space is found the reversed string is reverse agian
	//The word then is appened to s0 that will become the final output string.
	while(i < strlen(src)){
		if(src[i] == ' '){
			//Add end of string
			Buffer[j] = '\0';
			temp = strrev(Buffer);
			//Add end of string
			temp[j] = '\0';
			s0+= temp;
			//Append the space
			s0+= src[i];
			//Reset buffer counter
			j = 0;
		}
		else{
			//Getting building the string
			Buffer[j] = src[i];
			j++;
		}
		//INC Counter
		i++;
	}
	//This code just adds the remaining last string found in buffer
	Buffer[j] = '\0';
	temp = StrRev(Buffer);
	temp[j] = '\0';
	s0+= temp;
	s0+=c;

	//Clear up
	memset(Buffer,0,sizeof(Buffer));
	memset(src,0,sizeof(src));

	//Return new string
	return s0;
}

int main(int argc, char **anvg){
	string Org = "Microsoft Visual C++ For Windows";
	string s0 = RevWords("Microsoft Visual C++ For Windows");

	SetConsoleTitle(L"Word Reverse By ~DreamVB~");

	cout << "Original     : " << Org.c_str() << endl;
	cout << "Reversed     : " << s0.c_str() << endl;

	Org.clear();
	s0.clear();

	//Keep console open
	system("pause");
	return 1;
}