void loopMove(char* str,int n)
{
int i = 0;
char* temp = NULL;
int strLen = 0;
char *head = str; //指向字符串头部
while (*str++);
strLen = str - head - 1; //计算字符串长度
n = n % strLen; //计算字符串尾部移动到头部的字符个数
temp = (char*)malloc(n);
for (i = 0; i < n; i++)
{
temp[i] = head[strLen - n + i]; //临时存放从尾部移动到头部的字符
}
for (i = strLen - 1; i >= n; i--)
{
head[i] = head[i - n]; //从头部移动到尾部
}
for (i = 0; i < n; i++)
{
head[i] = temp[i]; //从临时内存区复制尾部字符
}
free(temp);
}
int main()
{
char str[] = "abcdefgbc";
loopMove(str, 4);
cout << str << endl;
system("pause");
return 0;
}
C编程实现字符串的循环右移
猜你喜欢
转载自blog.csdn.net/lpl312905509/article/details/104089773
今日推荐
周排行