Aim:
To insert a substring into a string.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[80],substr[20];
char X,newstr[80];
int pos,count1=0,count2=0,
int i=0,t=0,l;
clrscr ();
printf("Enter a string:");
gets(str);
printf("Enter a substring:");
gets(substr);
while( str[i] != '\0' )
{
count1++;
i++;
}
while( substr[i] != '\0' )
{
count2++;
i++;
}
printf("\nEnter the position to insert the substring");
scanf("%d",&pos);
strcpy(newstr,str);
l = pos+count2;
for( i=pos;i<=count1+count2;i++)
{
X = newstr[i];
if(t < count2)
{
str[i] = substr[t];
t = t+1;
}
str[l] = X;
l++;
}
printf("\nNewstring is: %s",str);
}
Program:
Enter a string: hydai
Enter a substring: un
Enter the position to insert the substring: 3
Newstring is hyundai
No comments:
Post a Comment