假定输入的字符串中只包含字母和*号。请编写函数fun,其功能是:除了尾部的*号之外,将字符中的其它的*号全部删除。
形参p已指向字符串中最后的一个字母。在编写函数时,不得使用C语言提供的字符串函数。
#include<stdio.h>
void fun(char a[],char *p)
{
char ch=*p;
p=a; //将p指向a
while(*a!=ch) //遍历a至最后一个字母,如果不是*就赋值给p
{
if(*a!='*')
{
*p++=*a;
}
a++;
}
while(*a!='\0') //将剩余的值赋给p
{
*p++=*a++;
}
a=p;
*(a++)='\0'; //将a的最后字符置'\0'
}
void main()
{
char s[81],*p;
p=s;
printf("please input a string:");
gets(s);
while(*p!='\0')p++;
p--;
while(*p=='*')p--; //将p指针指向最后一个字母
fun(s,p);
for(int i=0;s[i]!='\0';i++)
printf("%c",s[i]);
printf("\n");
}