1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4.  
  5. void reverse_block(char* p, int start, int len)
  6. {
  7. int a, b; a = b = 0;
  8. for (a = start, b = start+len; a <= b; a++, b--)
  9. {
  10. char tmp = p[a];
  11. p[a] = p[b];
  12. p[b] = tmp;
  13. }
  14. }
  15.  
  16. // I like pie
  17. // eip ekil I
  18. // pie like I
  19. void reverse_words(char* p)
  20. {
  21. // I like pie
  22. int len = strlen(p)-1;
  23. for (int idx = 0; idx < (len/2); idx++)
  24. {
  25. char tmp = p[idx];
  26. p[idx] = p[len-idx];
  27. p[len-idx] = tmp;
  28. }
  29. // eip ekil I
  30. int block_start = 0;
  31. int block_end = 0;
  32. for (int idx = 0; idx <= len+1; idx++)
  33. {
  34. if (p[idx] == ' ' || p[idx] == '\0')
  35. {
  36. block_end = idx-1;
  37. reverse_block(p, block_start, block_end - block_start);
  38. block_start = block_end + 2;
  39. }
  40. else
  41. {
  42. continue;
  43. }
  44. }
  45. }
  46.  
  47.  
  48. int main()
  49. {
  50. char buf[64];
  51. strcpy(buf, "Here is an example sentence for testing");
  52.  
  53. printf("%s\n", buf);
  54. reverse_words(buf);
  55. printf("%s\n", buf);
  56.  
  57. getchar();
  58.  
  59. return 0;
  60. }