Example: Implementing a string left shift function, such as "Abcdefghi" loop left 3 bits is "DEFGHIABC".
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h>void revers (char *left,char *right) //Flip function, left represents the start position, right represents the end position {assert (left), assert (right), char tmp = 0;while (Left < right) { TMP = *left;*left = *right;*right = tmp;left++;right--;}} void Leftloopmove (char *pstr, unsigned short steps) {assert (PSTR); int len = strlen (PSTR); steps = Steps%len; This can deal with the steps illegal situation revers (pstr,pstr+steps-1); First flip the front steps characters revers (pStr + steps, PStr + len-1); Then flip the remaining characters revers (PSTR, pStr + len-1); Flip the whole again} int main () {char arr[50] = {0};unsigned short steps = 0; The number of scanf to move right ("%s%hd", Arr,&steps), Leftloopmove (arr,steps);p rintf ("%s\n", arr); System ("pause"); return 0;}
Tips----"Three-step Flip method"