C language-K characters in the left-hand string
Problem: 3. Implement a function that can contain k characters in the left-hand string. AABCD: returns abcda aabcd, and returns BCDAA.
# 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 ("the number of input strings is too large \ 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 ("Enter the number of strings to be rotated \ n "); scanf_s ("% d", & s); reserve2 (str, len, s); printf ("% s", str );}