Example: Assume that the input string only contains letters and "#" signs. Please write the function fun. Its function is to move all the leading "#" signs in the string to the end of the string.
例如,若字符串中的内容为 “###a#b#c#d###”,
移动后,字符串中的内容应当是 “a#b#c#d######”。
在编写函数时,不得使用C语言提供的字符串函数。
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
代码如下:
#include<stdio.h>
#include<conio.h>
void fun(char*s)
{
int i=0,n=0;
char*p;
p=s;
while(*p=='#')
{
n++;
p++;
}
while(*p)
{
s[i]=*p;
i++;
p++;
}
while(n!=0)
{
s[i]='#';
i++;
n--;
}
s[i]='\0';
}
main()
{
char str[81],*p;
FILE*out;
char test[2][80]={
"###A###B#CD######","###a#b#c#d###"};
int i;
printf("Enter a string:");
gets(str);
fun(str);
printf("The string after moveing:");
puts(str);
out=fopen("outfile.dat","w");
for(i=0;i<4;i++)
{
fun(test[i]);
fprintf(out,"%s\n",test[i]);
}
fclose(out);
}
The output running window is as follows:
越努力越幸运!
加油,奥力给!!!