1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #include <stdio.h>
- /*
- rewrite reverse from chapter 3 to use pointers.
- */
- /* reverse: reverse string s in place */
- char* reverse(char* s);
- void testreverse();
- int main ()
- {
- testreverse();
- return(0);
- }
- void testreverse()
- {
- char p1[] ="123456789";
- printf("%s \n",reverse(p1));
- }
- char* reverse(char s[])
- {
- char* tmpstr=s;
- char* retstr=s;
- for (int i=0;*tmpstr!=0;i++)
- tmpstr++;
- tmpstr--; //back into the string
- char temp=0; //temp for swapping
- while (retstr!=tmpstr)
- {
- temp=*retstr;
- *retstr=*tmpstr;
- *tmpstr=temp;
- retstr++;
- tmpstr--;
- }
- return s;
- }
|