Wednesday, January 3, 2018

Inserting a string in another string at specific position

# include <stdio.h>
# include <conio.h>
# include <string.h>
main()
{
char str[50];
char istr[10];
char nstr[60];
int pos,i,j,k=0;
puts("\nEnter a long strings?");
gets(str);
puts("\nEnter  string to insert?");
gets(istr);
puts("\nEnter  pos?");
scanf("%d",&pos);
/* copy string up to pos */
for(i=0;i<pos;i++)
{
nstr[i]=str[i];
}
k=i;
/* copy istr to nstr */
for(j=0;istr[j]!=0; j++)
{
nstr[i]=istr[j];
i++;
}
/* copy rest of str to nstr */
       for(; str[k]!=0;k++,i++)
       {
       nstr[i]=str[k];
       printf("\n%c",str[k]);
       }
nstr[i]=0;
puts(nstr);
}
















No comments:

Post a Comment