C语言问题-高分求解

来源:百度知道 编辑:UC知道 时间:2024/05/22 01:43:32
题目:假定输入的字符串只包含字母和*号,请编写函数fun,它的功能是:使字符串中前部的*号不得多于n个,若多于n个,则删除多余的*号,若少于或等于n个,则什么也不做,字符串中间和尾部的*号,不删除。

例如:字符串为****A*BC*DEF*G******* 若n=2,删除后字符串变为: **A*BC*DEF*G*******

以下是我编写的程序
编译连接均没有错误,但得不到正确的结果,请高手指教

# include <stdio.h>
# include <conio.h>
void fun(char *a,int n)
{ int k=0,s=0;
char *p;
p=a;
while(*p)
{ if(*p=='*')
p++;
k++;
}
p=a;
while(*p)
{ p++;
s++;
}
if(k>n)
for(i=k-n,j=0;i<s;i++)
a[j++]=a[i];
a[j]='\0';

}

void main ()
{ char s[81];int n;
printf("Enter a string:\n");
gets(s);
printf("Enter n:");
scanf("%d",&n);
fun(s,n);
printf("The string after deleted :\n");
puts(s);
}
small0224和tanyuguo的答案都对,投票吧。

想来是我当时编程的时候太疲惫了,其实只是一个小问题,却耽误了那

i,j没声明,加上;

while(*p)
{ if(*p=='*')
p++;
k++;
}
改成
while(*p=='*')
{ p++;
k++;
}

# include <stdio.h>
# include <conio.h>
void fun(char *a,int n)
{ int k=0,s=0;
char *p;
p=a;
while(*p)
{ if(*p=='*')
p++;
k++;
}
p=a;
while(*p)
{ p++;
s++;
}
if(k>n){
int i,j;
for(i=k-n,j=0;i<s;i++)
a[j++]=a[i];
a[j]='\0';
}
}

void main ()
{ char s[81];int n;
printf("Enter a string:\n");
gets(s);
printf("Enter n:");
scanf("%d",&n);
fun(s,n);
printf("The string after deleted :\n");
puts(s);
}

int i,j;插入就行类
fun函数你没有写返回值1!!!!
运行是没有问题!!1
要结果还要改进fun函数!!!1
其实我也不怎么懂C 。
只能做到这个地步。。
交个朋友!!