Posted By

zava on 08/05/10


Tagged

textmate reverse


Versions (?)

reverse a string


 / Published in: C
 

  1. // Ansi string only!
  2. void reverse(char* s)
  3. {
  4. // Get the length of the string
  5. int len = 0;
  6. char* p = s;
  7. while (*p != 0)
  8. {
  9. len++;
  10. p++;
  11. }
  12.  
  13. // Swap ...
  14. int i = 0;
  15. char c;
  16. while (i <= len / 2 - 1)
  17. {
  18. c = *(s + i);
  19. *(s + i) = *(s + len - 1 - i);
  20. *(s + len - 1 - i) = c;
  21. i++;
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. char s[] = "Hello World!";
  28. printf("'%s' =>n", s);
  29. reverse(s);
  30. printf("'%s'n", s);
  31. return 0;
  32. }

Report this snippet  

You need to login to post a comment.