fork download
  1. #include<stdio.h>
  2. /* function to reverse a string */
  3. void reverse(char *start, char *end);
  4.  
  5. /*Function to reverse words*/
  6. void reversewords(char *s)
  7. {
  8. char *word = s;
  9. char *temp = s;
  10.  
  11. /*start reversing the individual words*/
  12. while( *temp )
  13. {
  14. temp++;
  15. if (*temp == '\0')
  16. {
  17. reverse(word, temp-1);
  18. }
  19. else if(*temp == ':')
  20. {
  21. reverse(word, temp-1);
  22. word = temp+1;
  23. }
  24. }
  25. reverse(s, temp-1);/*Reverse the resultant string from beginning to end including spaces*/
  26. }
  27. /*Function to reverse a string this is similar to earlier blog post to reverse a string */
  28. void reverse(char *start, char *end)
  29. {
  30. char temp;
  31. while (start < end)
  32. {
  33. temp = *start;
  34. *start++ = *end;
  35. *end-- = temp;
  36. }
  37. }
  38. int main()
  39. {
  40. char s[] = "23:24:28:10:29:18:12:24:11:27:10:22:10:20:28";
  41. char *temp = s;
  42. printf("Original string: %s\n",s);
  43. reversewords(s);
  44. printf("Reverse word string: %s\n", s);
  45. return 0;
  46. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
Original string: 23:24:28:10:29:18:12:24:11:27:10:22:10:20:28
Reverse word string: 28:20:10:22:10:27:11:24:12:18:29:10:28:24:23