字符串 字符以后的

来源:百度知道 编辑:UC知道 时间:2024/05/21 16:04:01
将字符串a的第n个字符后替换为b,请高手看一下

#include"stdio.h"
#include"string.h"
char strrep(char *a,char *b,int n)
{
char *q;
q=a+n;
while(*b);
*q++ = *b++;
*q='\0';
return a;
}
main()
{
char *a,*b,*c;
int n;
FILE *fp;
fp=fopen("test3-2.dat","w");
gets(a);
gets(b);
printf("please input the munber:");
scanf("%d",&n);
c=strrep(a,b,n);
puts(c);
fprintf(fp,"%s",c);
fclose(fp);
}

这个程序有错吧,gets()的参数必须是数组名,但是不能为指针变量,你用字符数组来获取字符串就对了
还有就是直接用strcpy来赋值字符串不就行了吗?
是strcpy( a[ n ],b )
前提是a足够长,如果不够长的话可以把a赋值到另一个较长字符数组里,然后赋值b

楼上已经说了几点,再补充一点
while(*b);
这个while循环的;应该去掉