本题要求编写函数,将输入字符串的前3个字符移到最后。
函数接口定义:
void Shift( char s[] );
其中char s[]
是用户传入的字符串,题目保证其长度不小于3;函数Shift
须将按照要求变换后的字符串仍然存在s[]
里。
void Shift(char s[])
{
int n = strlen(s);
if (n >= 3) { // 确保字符串长度至少为3
// 保存前3个字符
char b[3];
memcpy(b, s, 3 * sizeof(char));
// 将后面的字符向前移动3个位置
memmove(s, s + 3, (n - 3) * sizeof(char));
// 将保存的前3个字符放回末尾
memcpy(s + n - 3, b, 3 * sizeof(char));
}
}