[C Language] Left-hand string
To implement a function, you can use k characters in the left-hand string. Method 1: open another array and write the characters After k + 1 into this array first, write the k characters to be left-handed as follows: # include <stdio. h> # include <assert. h> # include <string. h> void left_Relvove (char * str, char * arr, int k) {char * pcur = str + k; // first let the pointer start from the k + 1 character, in the arr array, assert (str & arr); // assert while (* pcur) // starts with the k + 1 character, when '\ 0' jumps out of {* arr ++ = * pcur ++;} pcur = str; // writes the character after k + 1 to arr, adjust the pointer to the first element of str and start writing the k characters to be rotated while (k) {* arr ++ = * pcur ++; k --;} * arr = '\ 0';} int main () {int k; char str [10] = {0}; char arr [10] = {0 }; scanf ("% d", & k); scanf ("% s", str); left_Relvove (str, arr, k); printf ("% s \ n ", arr); return 0;} Method 2: Define a string flip function, first Reverse flip the k characters to be left, and then reverse flip the characters After k + 1, finally, flip the entire string in reverse order as follows: # include <stdio. h> # include <string. h> void reserve (char * left, char * right) {while (left <right) {char tmp = * left; * left = * right; * right = tmp; left ++; right -- ;}} int main () {char arr [10] = "AABCD"; int len = strlen (arr)-1; int k = 0; char * pstart = & arr [0]; char * pend = arr + len; scanf ("% d", & k); reserve (pstart, pstart + k-1 ); // first reverse the k characters to be left-handed to reserve (pstart + k, pend); // reverse the character after k + 1 to reserve (pstart, pend ); // Reverse flip of the entire string printf ("% s \ n", arr); system ("pause"); return 0 ;}