Remove the last occurrence of a character from 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;
}
void removeAll(char *str, const char toRemove)
{
int i, j;
int len = strlen(str);
for (i = 0; i < len; i++)
{
if (str[i] == toRemove)
{
for (j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
i--;
}
}
}
Output
Enter any string : I love Programming.I love Bangladesh
Enter character to remove from string : I
String after removing 'I' : I love programming.love Bangladesh
0 Comments