Write a C program that uses functions to perform the following operations:
i. To insert a substring into a given main string from a given position.
ii. To delete n characters from a given position in a given string.

Program Code i:
#include <stdio.h>
#include <string.h>
int main() {

int i,j,p,s1=0,s2=0,tem=0;
char str1[100],str2[100],temp[100];
printf("Enter the first string : ");
gets(str1);
printf("Enter the second string : ");
gets(str2);
printf("Enter the position to insert the second string : ");
scanf("%d",&p);
for(i=0;str1[i]!='\0';i++)
{
s1++;
}
for(i=0;str2[i]!='\0';i++)
{
s1++;
}
for(i=0;temp[i]!='\0';i++)
{
tem++;
}
j=0;
for(i=p;i<s1;i++)
{
temp[j] = str1[i];
j++;
}
str1[p] = '\0';
j=0;
for(i=p;i<(s1+s2);i++)
{
str1[i] = str2[j];
j++;
}
strcat(str1,temp);
puts(str1);
}

Program Code ii:
#include <stdio.h>
int main() {
int i,p,j,s1=0,n;
charstr[100],temp[100];
printf("Enter the string : ");
gets(str);
printf("Enter the position : ");
scanf("%d",&p);

printf("Enter the number of characters you want to delete : ");
scanf("%d",&n);
for(i=0;str[i]!='\0';i++) 
{
s1++;
}
j=0;
for(i=p+n;i<s1;i++) 
{
temp[j] = str[i];
j++;
}
str[p] = '\0';
j=0;
for(i=p;i<s1-p+n;i++) 
{
str[i] = temp[j];
j++;
}
puts(str);
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: