实现一个函数,可以左旋字符串中的k个字符。
例如:
ABCD左旋一个字符得到BCDA
ABCD左旋两个字符得到CDAB
void reverse(char* num, int first, int last)
{
int temp;
while (first <= last)
{
temp = num[first];
num[first] = num[last];
num[last] = temp;
first++;
last--;
}
}
void rotate(char* nums, int numsSize, int k){
k = k%numsSize;
reverse(nums, 0, numsSize - k - 1);
reverse(nums, numsSize - k, numsSize - 1);
reverse(nums, 0, numsSize - 1);
}
int main()
{
char c[] = "ABCDE";
int k = 2;
int len = strlen(c);
printf("左旋前:%s\n", c);
rotate(c, len, k);
printf("左旋后:%s\n", c);
system("pause");
return 0;
}