reverse the words sorting in a string
hi all i am trying to code somthing do this
reverse
life is nightmare
then prints like this
nightmare is life
here is my code but it's actually gives me a memory leak and not working propaply
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUF 250
main()
{
char buffer[BUF],word[BUF],*rev;
int x,g,len;
fgets(buffer,BUF,stdin);
len=strlen(buffer);
if((rev=malloc(len))==NULL)
{
fprintf(stderr,"Memory Allocating Error\n");
exit(1);
}
rev[len]='\0';
for(x=len-1,g=0;x>=1;x--,g++)
{
word[g]=buffer[x];
switch(buffer[x])
{
case ' ':
word[g]='\0';
strcat(rev,strrev(word));
g=0;
break;
}
}
printf("%s",rev);
return 0;
}
thanks in advance
and there is smothing else
i cannot do this by looking for spaces but i didn't fine any idea right now
thanks