Implement a function that can be left-handed in the string of K-character method one: Open another array, first write the character after k+1 to the array, and then write the K-character to the left to achieve the following: #include <stdio.h> #include < assert.h> #include <string.h>void left_relvove (char *str,char *arr,int k) {Char *pcur = str+k; //let the pointer begin with the K+1 character and save it in the ARR array assert (Str && arr); //assert while (*pcur) //starts with the k+1 character and encounters a ' \ ' jump out {*arr++ = *pcur++; }pcur = str; //after writing the character after k+1 to arr, move the pointer to the first element of STR, Start writing the k character to rotate while (k) {*arr++ = *pcur++;k--;} *arr= ' + ';} Int main () { int k; char str[10] = {0}; &NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;ARR[10]&NBSP;=&NBSP;{0};&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &k); scanf ("%s", str); left_relvove (str,arr,k); &NBSP;&NBSP;&NBSP;&NBSP;PRintf ("%s\n", arr); return 0;} Method Two: Define a string rollover function, the left-handed K-word Mr. Foo reverse reversal, and then the k+1 after the reverse reversal of the character, and finally the entire string reversed reverse implementation 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; &NBSP;&NBSP;&NBSP;SCANF ("%d", &k); reserve (pstart, pstart + k &NBSP;-1); //to the left-handed K word Mr. Foo reverse reversal reserve (pstart + k, pend); //k+1 The characters after the reverse reserve (pstart, pend); //the entire string reversed reverse printf ("%s\n", arr); system ("pause"); return 0;}
This article is from the "vs LV" blog, so be sure to keep this source http://survive.blog.51cto.com/10728490/1709777
"C language" left-handed string