5.6.c 605 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <stdio.h>
  2. /*
  3. rewrite reverse from chapter 3 to use pointers.
  4. */
  5. /* reverse: reverse string s in place */
  6. char* reverse(char* s);
  7. void testreverse();
  8. int main ()
  9. {
  10. testreverse();
  11. return(0);
  12. }
  13. void testreverse()
  14. {
  15. char p1[] ="123456789";
  16. printf("%s \n",reverse(p1));
  17. }
  18. char* reverse(char s[])
  19. {
  20. char* tmpstr=s;
  21. char* retstr=s;
  22. for (int i=0;*tmpstr!=0;i++)
  23. tmpstr++;
  24. tmpstr--; //back into the string
  25. char temp=0; //temp for swapping
  26. while (retstr!=tmpstr)
  27. {
  28. temp=*retstr;
  29. *retstr=*tmpstr;
  30. *tmpstr=temp;
  31. retstr++;
  32. tmpstr--;
  33. }
  34. return s;
  35. }