"C and Pointers"--6.3
Topic:
Write a function to reverse-arrange the characters in the argument string.
Function Prototypes:
void Reverse_string (char *string);
Requirements:
Use pointers instead of array subscripts
Do not use any function in the C function library to manipulate strings
Do not declare a local array to temporarily store parameter strings
Solution Code:
#include <stdio.h>voidReverse_string (Char*string){ intI, n=0; while(*(string+N)! =' /')//count the number of characters in a stringn++; N--;//The number of characters n as the index number should be reduced by 1, that is, from 0 to N-1 if(N >0)//There is no need to invert the number of characters equal to 1 o'clock { for(i=0; I<= (n/2); i++) { if(I! = (ni)) {CharP//Character Content Exchangep = * (string+H); *(string+i) = * (string+n-i); *(string+n-i) =p; } } }}intMain () {CharSource[] ="ABCDEFGH"; printf ("before reverse:\n%s\n", source); Reverse_string (source); printf ("After reverse:\n%s\n", source); GetChar (); return 0;}
Note:
1. Count the number of non-' s ' characters in the string first
2. The first character turns to Exchange
"C and Pointers" section post-programming exercise solution Reference--6.3