fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. void reverse_char_array(char * first, char * last) {
  4. while (first != last && first != --last) {
  5. *first ^= *last;
  6. *last ^= *first;
  7. *first++ ^= *last;
  8. }
  9. }
  10. void reverse_string(char * sentence) {
  11. reverse_char_array(sentence, &sentence[strlen(sentence)]);
  12. }
  13. void reverse_word(char * sentence) {
  14. char * end_s = &sentence[strlen(sentence)], * end_w = NULL;
  15. while (end_w != end_s) {
  16. if (!(end_w = strchr(sentence, ' '))) end_w = end_s;
  17. reverse_char_array(sentence, end_w);
  18. sentence = end_w + 1;
  19. }
  20. }
  21. int main(int argc, char ** argv) {
  22. char sentence[] = "Shall we all die? We shall die all! All die shall we? Die all we shall!";
  23. reverse_string(sentence);
  24. reverse_word(sentence);
  25. printf("%s\n", sentence);
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
shall! we all Die  we? shall die All  all! die shall We  die? all we Shall