Algorithm


 Declare variables:
   - inputString: array to store the input string from the user
   - outputString: array to store the modified string without certain characters
   - removalChar: character to be removed from the input string
   - i: loop variable
   
3. Input:
   - Prompt the user to enter a string and store it in inputString
   - Prompt the user to enter the character to be removed and store it in removalChar
   
4. Process:
   - Initialize i to 0
   - Initialize j to 0 (for the index of outputString)
   - Iterate through each character in inputString:
      - If the character is not equal to removalChar, then
         - Assign the character to outputString[j]
         - Increment j
      - Increment i
   
5. Output:
   - Display the modified string stored in outputString

Code Examples

#1 Example-C programing removes characters

Code - C Programming

/* C Program to Remove All Occurrences of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, len, j;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Remove :  ");
  	scanf("%c", &ch);
  	
	len = strlen(str);
	   	
  	for(i = 0; i < len; i++)
	{
		if(str[i] == ch)
		{
			for(j = i; j < len; j++)
			{
				str[j] = str[j + 1];
			}
			len--;
			i--;	
		} 
	}	
	printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
	
  	return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Please Enter any String : Hello
Please Enter the Character that you want to Remove : l
The Final String after Removing All Occurrences of 'l' = Heo

#2 Example-C Programing to Remove Character in a String

Code - C Programming

/* C Program to Remove All Occurrences of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, len, j;
  	i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Remove :  ");
  	scanf("%c", &ch);
  	
	len = strlen(str);
	   	
  	while(i < len)
	{
		if(str[i] == ch)
		{
			j = i;
			while(j < len)
			{
				str[j] = str[j + 1];
				j++;
			}
			len--;
			i--;	
		}
		i++; 
	}	
	printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
	
  	return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Please Enter any String : DevsEnv Tutorial
Please Enter the Character that you want to Remove : D
The Final String after Removing All Occurrences of 'D' = evsEnv Tutorial

#3 Code Example with C Programming

Code - C Programming

/* C Program to Remove All Occurrences of a Character in a String */
 
#include <stdio.h>
#include <string.h>

void Remove_AllOccurrence(char *str, char ch);
 
int main()
{
  	char str[100], ch;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Remove :  ");
  	scanf("%c", &ch);
  	
	Remove_AllOccurrence(str, ch);
		
	printf("\n The Final String after Removing All Occurrences of '%c' = %s ", ch, str);
	
  	return 0;
}

void Remove_AllOccurrence(char *str, char ch)
{
	int i, j, len;
	
	len = strlen(str);
	
	for(i = 0; i < len; i++)
	{
		if(str[i] == ch)
		{
			for(j = i; j < len; j++)
			{
				str[j] = str[j + 1];
			}
			len--;
			i--;	
		} 
	}	
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Please Enter any String : C programming languag
Please Enter the Character that you want to Remove : g
The Final String after Removing All Occurrences of 'g' = C prorammin lanuae
Advertisements

Demonstration


C Programing to Remove  a Character in a String-DevsEnv

Next
Appending into a File in C Programming