// Reverse character arrangement // vision 1.2 # include <stdio. h> void reverse_string (char * Str) {char * string; // the first character position char * last_char; // The last character position // For (last_char = STR ;; last_char ++) // If (* last_char = '\ 0') // break; // For (last_char = STR; * last_char! = '\ 0'; last_char ++) //;/*** set last_char to store the last character position */For (last_char = & STR [0]; * last_char! = '\ 0'; last_char ++); string = & STR [0]; last_char --; /*** increase the value of the pointer pointing to the position before and after switching and then decrease the pointer */while (string <last_char) {char temp; temp = * string; * string ++ = * last_char; * last_char -- = temp;} printf ("% s", STR);} int main (void) {char STR [10] = {"abcdef"}; reverse_string (STR); Return 0 ;}
If you do not use a library function, you can break out some functions in this library function. It's just a bit confusing. This is the final version, but in a previous version, the output is always only half the length of the source string, and it is also reversed. I don't understand it. I hope you can tell me when you see it. Thank you ~ The source code is as follows:
// Reverse character arrangement // vision 1.2 // Why is the program result incorrect? // At present, I guess the problem lies in the array pointer. // directly operate on the pointer of the original array, you should find an intermediate volume storage start pointer. // To be continued... # include <stdio. h> void reverse_string (char * Str) {char * last_char; // The last character position // For (last_char = STR; last_char ++) // If (* last_char = '\ 0') // break; // For (last_char = STR; * last_char! = '\ 0'; last_char ++) //;/*** set last_char to store the last character position */For (last_char = & STR [0]; * last_char! = '\ 0'; last_char ++); last_char --;/*** increase the pointer before and after the value of the pointer pointing to the position before and after switching */while (STR <last_char) {char temp; temp = * STR; * STR ++ = * last_char; * last_char -- = temp;} printf ("% s", STR);} int main (void) {char STR [10] = {"abcdxuf"}; reverse_string (STR); Return 0 ;}
There is only one more intermediate variable. Why?
The practice program of C on Point