171. C Program to Insert a Word in Exact Position in a Given text.
How does this program work?
- This Program is used to insert a word in exact location.
Here is the code
#include <string.h>
int main()
{
//To Insert a string in main string at exact position
char a[100];
char b[100];
char c[10];
int p =0,r =0,i=0;
int t =0;
int x,g,s,n,o;
puts("Enter Your String:\n");
gets(a);
puts("Enter String To add : \n");
gets(b);
printf("Enter the position where Word has to be inserted: \n");
scanf("%d",&p );
r = strlen(a);
n = strlen(b);
i =0;
// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;
// Adding the sub-string into the main string
for(i=p;i< s;i++)
{
x = c[i];
if(t< n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
}