给定一个字符串,要求把字符串前面的若干个字符移动到字符串的尾部,如把字符串“abcdef”前面的2个字符’a’和’b’移动到字符串的尾部,使得原字符串变成字符串“cdefab”。请写一个函数完成此功能,要求对长度为n的字符串操作的时间复杂度为 O(n),空间复杂度为 O(1)。?
void reverseStr(char *s, int from, int to){
while (from < to) {
char temp = s[from];
s[from++] = s[to];
s[to--] = temp;
}
}
void changePositionOfStr(char * s ,int p){
int n = (int)strlen(s);
p = p % n;
reverseStr(s, 0, p-1);
reverseStr(s, p, n-1);
reverseStr(s, 0, n-1);
}