fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void reverse(char*, int);
  5.  
  6. int main(int argc, char **argv)
  7. {
  8. char st[]= "hello world";
  9. int i=0, j=0;
  10.  
  11. while(st[j]){ //Loop till end of the string
  12. if ( st[j] == ' ') { //if you hit a blank space or the end of the string
  13. reverse(&st[i], j - i - 1); // reverse the string starting at position i till position before the blank space i.e j-1
  14. i = ++j; //new i & j are 1 position to the right of old j
  15. }
  16. else {
  17. j++; //if a chacacter is found, move to next position
  18. }
  19. }
  20. reverse(&st[i], j - i - 1);
  21.  
  22.  
  23. printf("%s\n",st);
  24. return 0;
  25. }
  26.  
  27. void reverse(char *s, int n)
  28. {
  29. char *end = s + n; //end is a pointer to an address which is n addresses from the starting address
  30. char tmp;
  31.  
  32. while (end > s) //perform swap
  33. {
  34. tmp = *end;
  35. *end = *s;
  36. *s = tmp;
  37. end--;
  38. s++;
  39. }
  40. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
olleh dlrow