6. a) Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to given main string from a given position.
ii) To delete n Characters from a given position in a given string.
Algorithm
i) To insert a sub-string in to given main string from a given position.
Step 1 : Start
Step 2 : Declare s, s1, s2, i, pos
Step 3 : Read String s
Step 4 : Read String s1
Step 5 : Read pos
Step 6 : initialize i=pos, j=0
Step 7 : if i<length of string s
7.1 : s2[j] = s[i]
7.2 : s[i]=s1[j]
7.3 : j=j+1, i=i+1
7.4 : goto step 7
Step 8 : concat s, s2
Step 9 : Print s
Step 10 : Stop
Program to delete ‘n’ characters from the given position of string
Algorithm:
Step 1 : Start
Step 2 : Declare s, s1, i, j, pos, n
Step 3 : Read String s
Step 4 : Read pos, n
Step 5 : initialize i=pos, j= pos + n
Step 6 : if j< length of string s
6.1 : s1[i] = s[j], s[i]=s1[i]
6.2 : j=j+1, i=i+1
6.3 : goto step 6
Step 7 : s[length-n]=end of string
Step 8 : Print s
Step 9 : Stop
Flowcharts
Program
i) To insert a sub-string in to given main string from a given position.
ii) To delete n Characters from a given position in a given string.
Algorithm
i) To insert a sub-string in to given main string from a given position.
Step 1 : Start
Step 2 : Declare s, s1, s2, i, pos
Step 3 : Read String s
Step 4 : Read String s1
Step 5 : Read pos
Step 6 : initialize i=pos, j=0
Step 7 : if i<length of string s
7.1 : s2[j] = s[i]
7.2 : s[i]=s1[j]
7.3 : j=j+1, i=i+1
7.4 : goto step 7
Step 8 : concat s, s2
Step 9 : Print s
Step 10 : Stop
Program to delete ‘n’ characters from the given position of string
Algorithm:
Step 1 : Start
Step 2 : Declare s, s1, i, j, pos, n
Step 3 : Read String s
Step 4 : Read pos, n
Step 5 : initialize i=pos, j= pos + n
Step 6 : if j< length of string s
6.1 : s1[i] = s[j], s[i]=s1[i]
6.2 : j=j+1, i=i+1
6.3 : goto step 6
Step 7 : s[length-n]=end of string
Step 8 : Print s
Step 9 : Stop
Flowcharts
![]() |
| 6 i |
![]() |
| 6 ii |
Program
i) To insert a sub-string in to given main string from a given position.
#include<stdio.h>
#include<string.h>
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
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
for(i=p;i{
x = c[i];
if(t {
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
}
ii) To delete n Characters from a given position in a given string.
#include<stdio.h>
#include<string.h>
void main()
{
char st[20],temp[20];
int pos,i,j,ct=0,n;
printf("Enter the string:");
gets(st);
printf("\nEntre the index position:");
scanf("%d",&pos);
printf("\nEnter the no.of characters:");
scanf("%d",&n);
if(pos<=strlen(st)) {
for(i=0;i<=strlen(st);i++) {
if(i==pos)
{
for(j=0;st[i]!='\0';j++) // to store the 'st' in 'temp' from a giv pos
{
temp[j]=st[i];
i++;
}
temp[j]='\0';
i=pos;
//to delete the 'n' char and after restore the temp in st.
for(j=0;temp[j]!='\0';j++)
{
ct++;
if(ct>n)
{
st[i]=temp[j];
i++;
}
}
st[i]='\0';
}
}
printf("\n\nAfter deletion the string: %s",st);
}
else
printf("\nSorry, we cannot delete from that position.");
}


Comments
Post a Comment