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
Let’s use the following algorithm and program to remove all occurrences of a character in a string using for loop, while loop and functions 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:
- Start program.
- Take input string from user, store it in some variable.
- Take Input character to remove from user, store it in some variable.
- Find and remove all occurrence of the given character in string.
- Print result
- 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