假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部移到字符串的尾部。
例如,字符串中的内容为:*******A*BC*DEF*G****,移动后,字符串中的内容应当是:A*BC*DEF*G***********。在编写函数时,不得使用C语言提供的字符串函数。
代码示例
#include<stdio.h>
void fun(char *a)
{
int i=0;
char *p,*q;
p=a;
while(*p=='*')
{
i++;
p++;
}
q=a;
while(*p)
{
*q=*p;
p++;
q++;
}
for(;i>0;i--)
*q++='*';
*q='\0';
}
int main()
{
char s[100];
int n=0;
printf("请输入只包含字母和*号的字符串:\n");
gets(s);
fun(s);
printf("移动后的字符串:\n");
puts(s);
}
运算结果: