Remove all occurrences of a character from a string with C.
#include <stdio.h>
#include <string.h>
/** Function declaration */
void removeAll(char *, const char);
int main()
{
char str[100];
char toRemove;
printf("Enter any string: ");
gets(str);
printf("Enter character to remove from string: ");
toRemove = getchar();
removeAll(str, toRemove);
printf("String after removing '%c': %s", toRemove, str);
return 0;
}
/**
* Function to remove all occurrences of a character from the string.
*/
void removeAll(char *str, const char toRemove)
{
int i, j;
int len = strlen(str);
for (i = 0; i < len; i++)
{
/*
* If the character to remove is found then shift all characters to one
* place left and decrement the length of string by 1.
*/
if (str[i] == toRemove)
{
for (j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
// If a character is removed then make sure i doesn't increments
i--;
}
}
}
Output
Enter any string : Learn With Rezaul Karim
Enter character to remove from string : e
String after removing 'e' : Larn With Rzaul Karim
0 Comments