C Program to Remove Given Word from String

C Program to Remove Given Word from String

Write a Program to Remove Given Word from String

// C Program to Remove Given Word from String

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

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


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

	printf("Enter String :--> ");
	gets(S1);

	int mainlength = strlen(S1);

	printf("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)
	{
		printf("Substring is located at index :--> %d",i);
		index = i;
		length = strlen(S2);
	}
	else
		printf("Substring not found");


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

	printf("\nUpdated String :--> %s", 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