編寫一個函式 reverse_string(char * string)(遞迴實現),將引數字串中的字元反向排列。 要求不能使用C函式庫中的字串操作函式
阿新 • • 發佈:2019-01-05
#include<stdio.h> #include<stdlib.h> int str(char *string) { int n = 0; while (*string) { n++; string++; } return n; } void reverse(char *string) { int len = str(string); if (*string) { char temp = *string; *string = *(string + len - 1); *(string + len - 1) = '\0'; reverse(string + 1); *(string + len - 1) = temp; } else { return; } } int main() { char s[10] = "abcdef"; reverse(s); printf(" %s\n", s); system("pause"); return 0; }