移动字母:
题目描述:
6-1 移动字母
本题要求编写函数,将输入字符串的前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; } /* 你的代码将被嵌在这里 */
思路:把前几个移动到后面的几个,相当于整体移动向前几个格子。
先写一个小的循环向前移,后面外面加大循环。
void Shift( char s[] )
{
int i,j;
char temp;
int l=strlen(s);
for(i=1;i<=3;i++)//大循环
{
temp=s[0];//s[0]与s[n-1]交换
for(j=0;j<l-1;j++)//向前推一步
{
s[j]=s[j+1];
}
s[l-1]=temp;
}
}