#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int reverseString(char str[]){
/* store length of str */
int l=strlen(str);
/* create buffer of equal size as str */
char *buffer=(char *)malloc(l*sizeof(char)+1);
if (!buffer)
return -1;
/* i sweeps over the entire string starting at the end */
int i=l-1;
/* dst to track where we copy characters into the buffer */
int dst=0;
/* end and src to identify the word we copy */
int end, src;
while (i>=0){
/* if we encounter a space, we copy it to buffer */
if (str[i]==' ')
buffer[dst++]=str[i--];
else{
/* identify end of word */
end=i;
/* find start of word */
while (str[i]!=' ' && i>=0)
i--;
src=i+1;
/* copy word*/
while (src<=end){
buffer[dst]=str[src];
src++; dst++;
}
}
}
/* null terminate buffer string */
buffer[l]='\0';
/* copy buffer over str */
strcpy(str, buffer);
free(buffer);
return 0;
}
int main (){
char str[]="To be or not to be, that is the question.";
if(reverseString(str)==-1){
return -1;
}
printf("%s",str);
return 0;
}