本题要求编写函数,将输入字符串的前3个字符移到最后。
函数接口定义:
void Shift( char s[] );
其中char s[]是用户传入的字符串,题目保证其长度不小于3;函数Shift须将按照要求变换后的字符串仍然存在s[]里。
裁判测试程序样例:
#include <stdio.h>
#include <string.h>
#define MAXS 10
void Shift( char s[] );
void GetString( char s[] ); /* 实现细节在此不表 */
int main()
{
char s[MAXS];
GetString(s);
Shift(s);
printf("%s\n", s);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
abcdef
输出样例:
defabc
void Shift( char s[] )
{
/*char a[3];
int i,j=0,n;
n=strlen(s);
for(i=0;i<3;i++)
a[i]=s[i];
for(i=n-3;s[i];i++)
{
s[j++]=s[i];
}
for(i=0;i<3;i++)
s[(n-3)++]=a[i];*/
int i,j;
char a[3];
a[0]=s[0];
a[1]=s[1];
a[2]=s[2];
for(i=3,j=0;;i++,j++)
{
if(s[i]!='\0')
s[j]=s[i];
else
{
s[j]=a[0];
s[j+1]=a[1];
s[j+2]=a[2];
break;
}
}
}