fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main(void) {
  5. char sentence [] = "this is a sentence";
  6. char *newSentence = (char *)malloc(strlen(sentence)+1);
  7. int i,j,start, k;
  8. start = 0;
  9.  
  10. for(i = 0;; i++)
  11. {
  12.  
  13. if(sentence[i] == ' ' || sentence[i] == '\0') //sentence[i] == '\0' for the last word.
  14. {
  15. char *word = (char *)malloc((i - start)+1);
  16. for(j = i-1, k = 0; j >= start; j--, k++)
  17. {
  18. word[k] = sentence[j];
  19. }
  20. word[k++] = ' '; //space after each word
  21. word[k] = '\0';
  22.  
  23. strcat(newSentence,word);
  24.  
  25. start = i+1;
  26. }
  27.  
  28. if (sentence[i] == '\0')
  29. break;
  30. }
  31. printf("%s\n",newSentence);
  32. return 0;
  33. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
siht si a ecnetnes