Given a sentence, reverse every word in that sentence.
Implementation:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Recursive function to reverse every word in a sentence */
void reverse(char *s)
{
if(*s=='\0')
return;
reverse(s+1);
printf("%c",*s);
}
int main(void) {
char *s,*v[3];
int j;
int i,k;
/* Allocate memory to get a sentence from the user */
s=(char*)malloc(sizeof(char));
/* Allocate memory to store N(here 3) words from a sentence */
for(i=0;i<3;i++)
*(v+i)=(char*)malloc(sizeof(char));
/* Get a sentence from the user */
gets(s);
/* Break the sentence into words and store it in a two dimensional array of characters */
for(i=0,k=0,j=0;*(s+j)!='\0';j++,k++)
{
//*(*(v+i)+k)=*(s+j);
if(*(s+j)==' ')
{
*(*(v+i)+k)='\0';
i++;
k=-1;
}
*(*(v+i)+k)=*(s+j);
}
/*for(i=0;i<3;i++)
printf("%s",*(v+i));*/
/* Call the function reverse for every word in the sentence */
for(i=0;i<3;i++)
{
reverse(*(v+i));
printf(" ");
}
return 0;
}