il please Write a function insert( ) that takes three arguments: a character vector str1[100], a character vector str2[100], and an integer i. Your function insert ( ) inserts the string str2 within string str1 after the ith element of str1 ( i is >= 0). For example, if str1="abcd" and str2="123" and i=0, after calling insert( ) the character vector str would contain "a123bcd". If str1="abcd" and str2="123" and i=1, after calling insert( ) the character vector str would contain "ab123cd". Character vectors str1 and str2 can hold strings of any length up to 99 characters. If the length of str1 after insterting str2 would be
I have the question and the answer what I want is to understand the logic of each line and explain the code to me in detail please
Write a function insert( ) that takes three arguments: a character vector str1[100], a character vector str2[100],
and an integer i. Your function insert ( ) inserts the string str2 within string str1 after the ith element of str1 ( i is >= 0).
For example, if str1="abcd" and str2="123" and i=0, after calling insert( ) the character vector str would contain "a123bcd".
If str1="abcd" and str2="123" and i=1, after calling insert( ) the character vector str would contain "ab123cd".
Character vectors str1 and str2 can hold strings of any length up to 99 characters. If the length of str1 after insterting str2 would be
larger than 99, place the string "ERROR" in str1
this is the answer
#include<stdio.h>
#include<string.h>
void insert(char *str1,char *str2,int i)
{
int x=0,y=0;
int len=0;;
int k=0;
char temp[100];
y=i;
while(str2[x]!='\0')
{
x++;
}
len=x;
x=0;
while(str1[y]!='\0')
{
y++;
temp[k++]=str1[y];
}
//printf("%s",str1);
y=i;
while(str1[y]!='\0')
{
y++;
while(str2[x]!='\0')
{
str1[y++]=str2[x];
x++;
}
}
k=0;
//printf("\n%s",temp);
while(temp[k]!='\0')
{
if(y>99)
{
strcpy(str1,"ERROR");
break;
}
str1[y++]=temp[k];
k++;
}
}
int main()
{
char str1[100],str2[100];
int i;
printf("\nEnter str1 :");
scanf("%s",&str1);
printf("\nEnter str2 :");
scanf("%s",&str2);
printf("\nEnter i :");
scanf("%d",&i);
insert(str1,str2,i);
printf("\nstr is : %s",str1);
return 0;
}
Step by step
Solved in 2 steps