Problem:
3. Implement a function that can be left-handed with the K characters in the string.
Aabcd left-handed one character gets ABCDA
Aabcd left two characters get 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);//dcbaaassert ( 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 strings entered is too large \ n");}} Void reserve2 (char* str, int len,int s) {assert (str); reserve1 (str, len, s); /cdbaachar* 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 ("Please enter the number of strings to rotate \ n"), scanf_s ("%d", &s), Reserve2 (str, len, s);p rintf ("%s", &NBSP;STR);}
This article from "The Soul of the Program" blog, declined reprint!
C--K characters in a left-handed string