Hello:)
Well this is my source code, but unfortunately it doesn't as it should. It should insert string s2 into s1 beggining from position p. For example if s1="green apple", s2="orange", p=3, output looks like this:
greorange
en aplle, and it should be in one line. I would appreciate very much if you could show me how to make it work properly. Thank you in advance!

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
main()
{char *s1,*s2,a[100],b[100],*s;
 int p;
 char *insert(char *s2,char *s1,int p);
 s1=fgets(a,100,stdin);
 s2=fgets(b,100,stdin);
 printf("Ucitaj poziciju p\n");
 scanf("%d",&p);
 s=insert(s2,s1,p);
 puts(s);
 getch();
}
char *insert(char *s2,char *s1,int p)
{ int i;
  char *pok;
  if (p>(strlen(s1)-1)) return s1;
  for(pok=s1+strlen(s1);pok>=(s1+p);pok--) *(pok+strlen(s2))=*pok;
  for(i=0;i<strlen(s2);i++)
   *(s1+p+i)=*(s2+i);
   return s1;

}

you could just call strcpy() to copy the contents of s2 into s1. Replace lines 21, 22 and 23 with this one line, unless of course your teacher said you can't use strcpy()

strcpy(s1+p, s2);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.