Remove the first occurrence of a character from the string with C.

#include <stdio.h>
#include <string.h>

/* Function declaration */
void removeFirst(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();

    removeFirst(str, toRemove);

    printf("String after removing first '%c' : %s", toRemove, str);

    return 0;
}

/**
 * Function to remove first occurrence of a character from the string.
 */
void removeFirst(char *str, const char toRemove)
{
    int i = 0;
    int len = strlen(str);

    /* Run loop till the first occurrence of the character is not found */
    while (i < len && str[i] != toRemove)
        i++;

    /* Shift all characters right to the position found above, to one place left */
    while (i < len)
    {
        str[i] = str[i + 1];
        i++;
    }
}

Output


Enter any string : I love Programming.I love Bangladesh
 Enter character to remove from string : I
String after removing first 'I' : love Programming.I love Bangladesh