For example: "Abcdefghi" loop right Shift 2 bits is: "HIABCDEFG" given function prototype: void Rightloopmove (char *pstr,unsigned short Steps) method One: First, the last word assigned to a temporary variable, Move the preceding character back one position in turn, and finally assign the temporary variable to the first character.
#include <stdio.h>#include <string.h>voidRightloopmove (Char*PSTR,unsigned ShortSteps) {Char*l = PSTR;Char*r = PStr +strlen(PSTR)-1; while(steps) {CharTMP = *R;Char*p = R; while(P > L) {*p = * (P-1); p--; } *l = tmp; steps--; }}intMain () {CharA[] ="Abcdefghi"; Rightloopmove (A,2);printf("%s\n", a);return 0;}
Method Two: Flip the string three times
#include <stdio.h>#include <string.h>voidReverse(Char*l, Char*r){ while(L < R) {Char TMP =*l;*l=*r;*r= tmp; l++; r--; }}void Rightloopmove (Char*pstr, unsigned short steps) {intn = strlen (PSTR);Reverse(PStr, pstr+n-steps-1);Reverse(PStr + n-steps,pstr+n-1);Reverse(pstr,pstr+n-1);}intMain () {char a[] ="Abcdefghi"; Rightloopmove (A,2);printf('%s\ n ', a);return 0;}
"C Language" string right loop shift