#include <stdio.h>
#include <string.h>

void reverse(char*, int);

int main(int argc, char **argv)
{
	char st[]= "hello world";
	int len = strlen(st);
	int i=0,j=0;
	
	reverse(st,len-1); // Reverse the entire string. hello world => dlrow olleh
	
	while(st[j]){ //Loop till the string ends
		if (*(st+j)==' ') { //if you hit a blank space 
			reverse(st+i,j-i-1); // reverse the string starting at position i till position j
			i=++j; //new i & j are 1 position to the right of old j
		}
		else {
			j++; //if a chacacter is found, move to next position
		}		
	}		
	reverse(st+i,j-i-1); // reverse the last word of the string
	
	printf("%s",st);
	return 0;
}

void reverse(char *s, int n)
{
	char *end = s+n; //end is a pointer to an address which is n addresses from the starting address
	char tmp;
	while (end>s)  //perform swap
	{
		tmp = *end;
		*end = *s;
		*s = tmp;
		end--;
		s++;
	}
}