Home > Back-end >  About C language program, a great god help solve
About C language program, a great god help solve

Time:10-24

The rotate
Every character string "abcd" shift to the right, the right to move to the position of the first character, become "dabc", which is referred to as a string for rotation displacement=1, by the same token, the "abcd" to: "cdab"=2 is called displacement rotation,
The code is as follows:
1 # include & lt; String. H>
2 # include & lt; Stdlib. H>
3 void shift (char * s, int n)
4 {
Char * p;
6 char * q;
7 int len=strlen (s);
Char * s2;
9? If (len==0) return;
10 if (n<=0 | | n>=len) return;
11=s2 (char *) malloc (sizeof (char) * (len + 1));
12? P=s;
13 q=s2 + n % len;
14 the while (* p)
15 {
16 * q++=* p++;
17 if (q - s2 & gt;=len)
18 {
19 * q='\ 0';
20 q=s2;
21}
22? }
Strcpy (s, s2);
24 free (s2);
25}
26 void main ()
27 {
28 char STR [10]="zhanghe";
29 shift (STR, 2);
30 printf (" % s \ n ", STR);
31}

11 what behavior is len + 1 excuse me?
Lines 12 13 is what meaning, p and q represent?
Line 17 what meaning be?

CodePudding user response:

Line 11 is to string s2 dynamic application space, because the last string is' \ 0 ', so to add 1, or you'll spill
Line 12 p represents the need to move the original string of the first character of the
Line 13 q point was the location of the last in the round
Line 17 determine whether move the last

CodePudding user response:

The 11 lines look very nervous, sizeof the result is unsigned, but there is a symbol of len;
  • Related