当初想了半天没想出来。。(脑子太笨了。。。。T.T)
回家仔细考虑了下。 实现如下:
void move_string(char *msg, int steps) { int len; int pos; int head; char tmp; int count; if(msg == NULL)return; len = strlen(msg); steps = steps % len; head = 0; count = 0; while(1){ tmp = msg[head]; if(pos + steps >= len){ count++; } pos = (pos + steps) % len; // back to head do next loop. if(pos == head){ if(count == steps){ break; } head++; pos = head; continue; } msg[head] = msg[pos]; msg[pos] = tmp; } printf("the result : %s\n", msg); }
时间: 2024-11-11 10:14:02