C++ Program to Insert Substring at Specific Location
Write C++ Program to Insert Substring at Specific Location
// CPP Program to Insert Substring at Specific Location
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
char S1[50], S2[15];
int index, i, l1, l2;
cout << "Enter String :--> ";
gets(S1);
cout << "Enter Substring :--> ";
gets(S2);
cout << "Enter Index :--> ";
cin >> index;
l1 = strlen(S1);
l2 = strlen(S2);
if(index > l1)
{
cout << "Invalid Index";
exit(0);
}
for(i = l1;i >= index; i--)
S1[i + l2] = S1[i];
for(i = 0;i < l2; i++)
S1[index + i] = S2[i];
cout << "\nAfter Insertion :--> " << S1;
}
Output:
Enter String :--> Hi CodeCrucks
Enter Substring :--> and Hello
Enter Index :--> 3
After Insertion :--> Hi and Hello CodeCrucks