Description:
in this program
we need to insert a string into another string from a specified position.
Algorithm:
Step 1: start
Step 2: read main string and sub
string
Step 3: find the length of main
string(r)
Step 4: find length of sub
string(n)
Step 5: copy main string into sub
string
Step 6: read the position to insert
the sub string( p)
Step 7: copy sub string into main
string from position p-1
Step 8: copy temporary string into
main string from position p+n-1
Step 9: print the strings
Step 10: stop
Program:
#include <stdio.h>
#include <conio.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;
clrscr();
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<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
getch();
}
Output:
1.enter first string:
computer
2.enter second string:
gec
3.enter the position where the item has to be inserted:3
comgecputer
conclusion :
the program is error free
VIVA QUESATIONS:
1) What
is string ?
Ans: A string is an collection of
characters
2) Which
command is used to combined the two strings ?
Ans: Strcat()
3) Which
command is used to copy the strings ?
Ans: By using the strcpy()
function copies one string to another
No comments:
Post a Comment