规定输入的字符串中只含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部删除,中间和结尾的*号不删除。
例如,若字符串中的内容为*******A*BC*DEF*G****,删除后,字符串中的内容则应当是A*BC*DEF*G****。在编写函数时,不得使用C语言提供的字符串函数。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#include
void fun(char *a)
{
}
main()
{
char s[81];
FILE *out;
printf("Enter a string :n");
gets(s);
fun(s);
printf("The string after deleted :n");
puts(s);
out=fopen("out.dat","w");
strcpy(s, "*******A*BC*DEF*G****");
fun(s);
fprintf(out, "%s", s);
fclose(out);
}
参考答案:
void fun ( char *a)
{
int i=0;
char *p=a;
while (*p&&*p==’*’)
p ;
while (*p)
{
a[i]=*p;
i ;
p ;
}
a[i]=’ ’;
}
