C Program to Delete Given Character From String

C Program to Delete Given Character From String

Write C Program to Delete Given Character From String

// C Program to Delete Given Character From String

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

int main()
{
	char S[] = "Hello and Welcome to CodeCrucks", ch;
	int n;

	printf("Original String :--> %s", S);

	printf("\nEnter Character to Remove :--> ");
	scanf("%c", &ch);

	n = strlen(S);

	int i = 0, flag = 0;

	while(i < n)
	{
	 	if(S[i] == ch)
	 	{
	 		flag = 1;
	 		int j = i;
	 		while(j < (n - 1))
	 		{
	 			S[j] = S[j + 1];  //shift the elements to 1 step left to the index i;
	 			j++;
      		}
			n--;
			i--;
		 }
		 i++;
	}
	S[i] = NULL;

	if(flag == 0)
	{
		printf("Element is not present in the array");
	}
	else
	{
		printf("\nString After Deleting %c :--> %s", ch, S);
	}

	return 0;
}

Output:

Original String :--> Hello and Welcome to CodeCrucks
Enter Character to Remove :--> e

String After Deleting e :--> Hllo and Wlcom to CodCrucks