Write a function insert() that takes three arguments: a character vector str1 [1
ID: 3852156 • Letter: W
Question
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 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.Explanation / 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]!='')
{
x++;
}
len=x;
x=0;
while(str1[y]!='')
{
y++;
temp[k++]=str1[y];
}
//printf("%s",str1);
y=i;
while(str1[y]!='')
{
y++;
while(str2[x]!='')
{
str1[y++]=str2[x];
x++;
}
}
k=0;
//printf(" %s",temp);
while(temp[k]!='')
{
if(y>99)
{
strcpy(str1,"ERROR");
break;
}
str1[y++]=temp[k];
k++;
}
}
int main()
{
char str1[100],str2[100];
int i;
printf(" Enter str1 :");
scanf("%s",&str1);
printf(" Enter str2 :");
scanf("%s",&str2);
printf(" Enter i :");
scanf("%d",&i);
insert(str1,str2,i);
printf(" str is : %s",str1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.