C Program To Remove All Occurrences of a Character in a String

In this tutorial, i am going to show you how to remove all occurrences of a character in a string with the help of for loop, while loop and functions in c programs.

All C Programs and Algorithm to Remove All Occurrence of a Character in a String

  • Algorithm to remove all occurrence of a character
  • C Program to Remove All Occurrence of a Character in a String using For Loop
  • C Program to Remove All Occurrence of a Character in a String using While Loop
  • C Program to Remove All Occurrence of a Character in a String using Function

Algorithm to remove all occurrence of a character

Follow the below given algorithm to write a c program to remove all occurrences of a character in a given string; as follows:

  1. Start program.
  2. Take input string from user, store it in some variable.
  3. Take Input character to remove from user, store it in some variable.
  4. Find and remove all occurrence of the given character in string.
  5. Print result
  6. End program

C Program to Remove All Occurrence of a Character in a String using For Loop

/* 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;
}

The result of the above c program; as follows:

Please Enter any String :  hello world
Please Enter the Character that you want to Remove :  o
The Final String after Removing All Occurrences of 'o' = hell wrld 

C Program to Remove All Occurrence of a Character in a String using While Loop

/* 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;
}

The result of the above c program; as follows:

Please Enter any String :  c program
Please Enter the Character that you want to Remove :  r
The Final String after Removing All Occurrences of 'r' = c pogam 

C Program to Remove All Occurrence of a Character in a String using Function

/* 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--;	
		} 
	}	
}

The result of the above c program; as follows:

Please Enter any String :  testing
Please Enter the Character that you want to Remove :  t
The Final String after Removing All Occurrences of 't' = esing

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *