Program to insert & delete sub string into/from main string from a given position.
13a). Insert a sub string into main string from a given position.
Source Code:
#include<stdio.h>
#include<string.h>
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,a1=0,i=0;
int t=0;
int x,g,s,b1,O;
printf("enter first string:\n");
scanf("%s",a);
printf("enter second string:\n");
scanf("%s",b);
printf("enter the position where the item has to be inserted:\n");
scanf("%ls",&p);
a1=strlen(a);
b1=strlen(b);
i=0;
while(i<=a1)
{
c[i]=a[i];
i++;
}
s=a1+b1;
O=p+b1;
for(i=p;i<s;i++)
{
x=c[10];
if (t<b1)
{
a[i]=b[t];
t=t+1;
}
a[0]=x;
O=O+1;
}
printf("New string is: %s\n",a);
}
Output:
enter first string:
welcome
enter second string:
go
enter the position where the item has to be inserted:
3
welcome
enter second string:
go
enter the position where the item has to be inserted:
3
New string is :
welgocome
13b). Delete sub string from main string from a given position.
Source Code:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[100];
int i,n,l,pos;
printf("enter the string1 :\n");
gets(str1);
printf("Enter the position where the characters are to be delete:\n");
scanf("%d",&pos);
printf(" Enter the no. of characters to be delete:\n");
scanf("%d",&n);
l=strlen(str1);
for(i=pos+n;i<l;i++)
{
str1[i=n]=str1[i];
}
str1[i-n]='\0';
printf("the String1 is:%s\n",str1);
}
#include<string.h>
void main()
{
char str1[100];
int i,n,l,pos;
printf("enter the string1 :\n");
gets(str1);
printf("Enter the position where the characters are to be delete:\n");
scanf("%d",&pos);
printf(" Enter the no. of characters to be delete:\n");
scanf("%d",&n);
l=strlen(str1);
for(i=pos+n;i<l;i++)
{
str1[i=n]=str1[i];
}
str1[i-n]='\0';
printf("the String1 is:%s\n",str1);
}
Output:
enter the string1 :
welcome
Enter the position where the characters are to be delete:
0
Enter the no. of characters to be delete:
3
welcome
Enter the position where the characters are to be delete:
0
Enter the no. of characters to be delete:
3
the String1 is:
come
No comments:
Post a Comment