C++ Program to Remove Given Word from String

C++ Program to Remove Given Word from String

Write C++ Program to Remove Given Word from String

// CPP Program to Remove Given Word from String

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	char S1[50], S2[50];


	int index, length, i, l1, l2, Max;

	cout << "Enter String :--> ";
	gets(S1);

	int mainlength = strlen(S1);

	cout << "Enter Substring :--> ";
	gets(S2);

	l1 = strlen(S1);
	l2 = strlen(S2);
	Max = l1 - l2;

	for(i = 0;i <= Max; i++)
	{
		if(strncmp(S1 + i, S2, l2) == 0)
            /* here S1 and S2 is considered as array address and (S1 + i)
            means array is compared with array b up to the size of substring */
			break;
	}
	if(i <= Max)
	{
		cout << "Substring is located at index :--> " << i;
		index = i;
		length = strlen(S2);
	}
	else
		cout << "Substring not found";


	if(index + length > mainlength)
	{
		cout << "Sorry the index is out of range to delete ";
	}
	else
	{
		for(i = index; i < mainlength; i++)
		    S1[i] = S1[i + length];
	}

	cout << "\nUpdated String :--> " << S1;

	return 0;
}

Output:

Enter String :--> Hello and Welcome to CodeCrucks
Enter Substring :--> and
Substring is located at index :--> 6
Updated String :--> Hello Welcome to CodeCrucks