实现字符串右循环移位函数,例如“abcdef”循环右移2位就是“cdefab”。#include
#include
void left_move(char arr[], int k)
{
int i = 0;
int len = strlen(arr);
for (i = 0; i
{
char tmp = arr[0];
int j = 0;
for (j = 0; j
{
arr[j] = arr[j + 1];
}
arr[len - 1] = tmp;
}
}
int main()
{
char arr[] = "abcdef";
int k = 0;
scanf("%d", &k);
left_move(arr, k);
printf("%s\n", arr);
system("pause");
return 0;
}
//法2:三步翻转法
#include
void reverse(char *left, char *right)
{
while (left
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void left_move(char arr[], int k)
{
int len = strlen(arr);
reverse(arr + 0, arr + k - 1);
reverse(arr + k, arr + len - 1);
reverse(arr + 0, arr + len - 1);
}
int main()
{
char arr[] = "abcdef";
int k = 0;
scanf("%d", &k);
left_move(arr, k);
printf("%s\n", arr);
system("pause");
return 0;
}