Sunday, 12 December 2010

Program for Functions to insert a sub string into given main string from a given position

Description:


In this program we need to insert a string into another string from a specified position.


Program:


#include<stdio.h>
#include<conio.h>
#include <string.h>

void main()
{
Char a[10], b[10], c[10];

int p=0,r=0,i=0,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: get
3.enter the position where the item has to be inserted:3 comgetputer

Conclusion :

The program is error free


VIVA QUESTIONS:

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: strcpy() function copies one string to another




c programs

No comments:

Post a Comment