求高手 用指针编写函数: insert(s1,s2,f), 其功能是在字符串s1中的指定位置f处插入字符串s2.
#include<stdio.h>insert(char*s1,int*s2,intn){inti=0;char*ss=char[100];for(i=0;i<n;i++...
#include<stdio.h>
insert(char *s1,int *s2,int n)
{
int i=0;
char *ss=char[100];
for(i=0;i<n;i++)
*ss=*s1++;
while(*s2!='\0')
*ss++=*s2++;
while(*s1!='\0')
{
*ss++=*s1++;
}
*ss='\0';
}
void main()
{char *ss;
int f;
char s1[]="we are fghjk";
char s2[]="riend";
scanf("%s%s",&s1,&s2);
char *ss=insert(s1,s2,f);
printf("%s",ss);
}
这是我编的 有两个语法错误 帮改改错也可以 展开
insert(char *s1,int *s2,int n)
{
int i=0;
char *ss=char[100];
for(i=0;i<n;i++)
*ss=*s1++;
while(*s2!='\0')
*ss++=*s2++;
while(*s1!='\0')
{
*ss++=*s1++;
}
*ss='\0';
}
void main()
{char *ss;
int f;
char s1[]="we are fghjk";
char s2[]="riend";
scanf("%s%s",&s1,&s2);
char *ss=insert(s1,s2,f);
printf("%s",ss);
}
这是我编的 有两个语法错误 帮改改错也可以 展开
2个回答
展开全部
//这个题我昨天回答过,你们问的不是同一个吧?
#include<stdio.h>
void insert(char *s1,char *s2,int f)
{
int i,j;
char t;
for(i=f,j=0;s2[j];i++,j++)
{
t=s1[i];
s1[i]=s2[j];
s2[j]=t;
}
for(j=0;s2[j];j++,i++)
s1[i]=s2[j];
s1[i]='\0';
}
int main(void)
{
char s1[81]="abcdefgh",s2[]="kkkkkkkk";
insert(s1,s2,3);
printf("%s\n",s1);
return 0;
}
#include<stdio.h>
void insert(char *s1,char *s2,int f)
{
int i,j;
char t;
for(i=f,j=0;s2[j];i++,j++)
{
t=s1[i];
s1[i]=s2[j];
s2[j]=t;
}
for(j=0;s2[j];j++,i++)
s1[i]=s2[j];
s1[i]='\0';
}
int main(void)
{
char s1[81]="abcdefgh",s2[]="kkkkkkkk";
insert(s1,s2,3);
printf("%s\n",s1);
return 0;
}
追问
那我就不知道了 也许是一个学校的吧!!
能帮看看我变得错哪了吗???能不能编成自动在f处植入字符串
多谢!!
追答
#include
char ss[100];
void insert(char *s1,char *s2,int n)
{
int i=0;char *p=ss;
for(i=0;i<n;i++)
*p++=*s1++;
while(*s2!='\0')
*p++=*s2++;
while(*s1!='\0')
{
*p++=*s1++;
}
*p='\0';
}
void main()
{
int f=8;
char s1[100]="we are fghjk";
char s2[]="riend";
insert(s1,s2,f);
printf("%s",ss);
}
展开全部
#include <stdio.h>
void insert(char *,char *,int);
int main()
{
char str1[100] = "This is a insert demo using pointer!";
char str2[10] = "Hello";
int n;
printf("str1 is:\n%s\n\n",str1);
printf("str2 is:\n%s\n\n",str2);
printf("input n:\n");
scanf("%d",&n);
insert(str1,str2,n);
printf("\n\nstr1 is:\n%s\n",str1);
return 0;
}
void insert(char *s1,char *s2,int f)
{
char *p;
char *q;
q = s2;
while(*q)
{
q++;
}
p = s1;
while(*p)
{
p++;
}
while(p-s1>=f)
{
*(p+(q-s2)) = *p;
p--;
}
p++;
q = s2;
while(*q)
{
*p = *q;
p++;
q++;
}
}
void insert(char *,char *,int);
int main()
{
char str1[100] = "This is a insert demo using pointer!";
char str2[10] = "Hello";
int n;
printf("str1 is:\n%s\n\n",str1);
printf("str2 is:\n%s\n\n",str2);
printf("input n:\n");
scanf("%d",&n);
insert(str1,str2,n);
printf("\n\nstr1 is:\n%s\n",str1);
return 0;
}
void insert(char *s1,char *s2,int f)
{
char *p;
char *q;
q = s2;
while(*q)
{
q++;
}
p = s1;
while(*p)
{
p++;
}
while(p-s1>=f)
{
*(p+(q-s2)) = *p;
p--;
}
p++;
q = s2;
while(*q)
{
*p = *q;
p++;
q++;
}
}
更多追问追答
追问
好厉害!!!但是题中说在f处植入字符串 能不能编成自动在f处植入字符串
追答
植入?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询