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

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

C program to remove all occurrences of a character in a string; Through this tutorial, we will learn how to remove all occurrences of a character in a string using for loop, while loop and functions in c programs.

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

  • 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

Use the following 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 output 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 output 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 output 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

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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