Insertion of a string program using c
Insertion of a string program
#include<stdio.h>#include<conio.h>
#include<string.h>
void main()
{
char main1[50],save[50],ins[50];
int i,j,lens,lens1,pos,len2,len,last,len3;
clrscr();
printf("Enter the main string:");
gets(main1);
printf("Position from where the string is to be insertd:\n\n");
scanf("%d",&pos);
printf("Enter the string :\n\n");
scanf("%s",ins);
lens1=strlen(main1);
len2=strlen(ins);
len=lens1+len2;
if(pos>lens1)
{
for(i=0;i<len2;i++)
{
main1[pos]=ins[i];
pos++;
}
}
else
{
last=pos+len2-1;
j=();
for(i=pos-1;i<lens1;i++)
{
save[j]=main1[i];
j=j+1;
}
}
len3=j;
i=pos-1;
for(j=0;j<len2;j++)
{
main1[1]=ins[j];
i=i+1;
}
main1[last]='';
last=last+1;
for(i=0;i<len3;i++)
{
main(last]=save[i];
{
main1[last]='\0';
}
printf("The string is \n");
for(i=0;i<len+1;i++)
{
printf("%c",main1[i]);
}
getch();
}
Output
Enter the main string: God LovePositin from where the string is to be inserted:5
Enter the string:Is
The string is :God is love
No comments: