fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char *reverse(char *str)
  5. {
  6. char tmp, *src, *dst;
  7. size_t len;
  8.  
  9. if (str != NULL)
  10. {
  11. len = strlen(str);
  12. if (len > 1)
  13. {
  14. src = str;
  15. dst = src + len - 1;
  16.  
  17. while (src < dst)
  18. {
  19. tmp = *src;
  20. *src++ = *dst;
  21. *dst-- = tmp;
  22. }
  23. }
  24. }
  25. return str;
  26. }
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30. char *str[] = {"a", "ab", "abc", "test", "another test"};
  31. int i;
  32. char s[10000];
  33. for (i=0; i < sizeof(str) / sizeof(str[0]); i++)
  34. {
  35. strcpy (s, str[i]);
  36. printf("Original: '%s'\n", str[i]);
  37. printf("Reversed: '%s'\n", reverse(s));
  38. }
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Original: 'a'
Reversed: 'a'
Original: 'ab'
Reversed: 'ba'
Original: 'abc'
Reversed: 'cba'
Original: 'test'
Reversed: 'tset'
Original: 'another test'
Reversed: 'tset rehtona'