#include<stdio.h> #include<assert.h> #include<string.h> void reserve(char* str, int len) { assert(str); char* start = str; char* end = str + len - 1; while (start < end) { char tmp = *start; *start = *end; *end = tmp; start++; end--; } } void reserve1(char* str, int len, int k) { reserve(str, len);//dcbaa assert(str); char* start = str ; if (k < len) { char* end = str + len - 1 - k; while (start < end) { char tmp = *start; *start = *end; *end = tmp; start++; end--; } } else { printf("輸入的字符串個數太大\n"); } } void reserve2(char* str, int len,int s) { assert(str); reserve1(str, len, s);//cdbaa char* start = str+len-s; char* end = str + len - 1; while (start < end) { char tmp = *start; *start = *end; *end = tmp; start++; end--; } } int main(void) { char str[] = "aabcd"; int len = strlen(str); int s; printf("請輸入要旋轉的字符串個數\n"); scanf_s("%d", &s); reserve2(str, len, s); printf("%s", str); }