加粗样式本体要求编写函数,将输入字符串的前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
代码:
#include <stdio.h>
#include <string.h>
#define M 10
void Shift( char s[]);
void GetString( char s[]);
int Main()
{
char s[M];
GetString(s);
Shift(s);
printf("%s\n",s);
return 0;
}
void Shift( char s[])
{
int i,j,t;
char ch[100];
t = strlen(s);
for(i = 0;i < 3 ;i++ )
{
ch[i] = s[i];//存放前三个字符;
}
for(i = 3;i < t;i++)
{
s[i - 3] = s[i];//除去前三个所有的字符向前移动
}
for(j = 0,i = t - 3;j < 3;i++,j++)
{
s[i] = ch[j];//前三个移动到后边
}
}
void GetString( char s[])
{
gets(s);
}
小编还是一名菜鸡,在PTA上运行不知哪里出错,还望大佬指点。