C program to remove all occurrences of a character from string

Category: C Program

Learn how to write a C program to remove all occurrences of a specified character from a string. This guide includes step-by-step instructions, code implementation, and detailed explanations for beginners in C programming.

In this article, we will discuss how to write a C program that removes all occurrences of a specified character from a given string. This operation is common in text processing tasks such as cleaning data, formatting text, and editing content.

Objective

The goal is to create a C program that takes a string and a character as inputs, removes all occurrences of the specified character from the string, and then prints the modified string.

Approach

To solve this problem, we will:

  1. Read the input string from the user.
  2. Read the character to be removed from the user.
  3. Traverse the string to remove all occurrences of the specified character.
  4. Print the modified string.

Detailed Steps

  1. Read the input string:
    • Use standard input functions to get the string from the user.
  2. Read the character to be removed:
    • Use standard input functions to get the character from the user.
  3. Remove all occurrences of the character:
    • Traverse the string, and for each occurrence of the specified character, shift the subsequent characters to the left.
  4. Print the modified string:
    • Print the resulting string after the character has been removed.

Write a C program to remove all occurrences of a character from string

Below is the complete C program that implements the above steps:

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

int main() {
    char str[100];
    char char_to_remove;
    int i, j;

    // Input the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character from string if present
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }

    // Input the character to remove
    printf("Enter the character to remove: ");
    scanf("%c", &char_to_remove);

    // Remove all occurrences of the character
    for (i = 0, j = 0; i < strlen(str); i++) {
        if (str[i] != char_to_remove) {
            str[j++] = str[i];
        }
    }
    str[j] = '\0';

    // Print the modified string
    printf("String after removing all occurrences of '%c': %s", char_to_remove, str);

    return 0;
}

Output

Enter a string: learn programming at ProCoding
Enter the character to remove: a
String after removing all occurrences of 'a': lern progrmming t ProCoding

Explanation of the Program

  1. Input Handling:
    • The program uses fgets() to read a line of text from the user, ensuring that spaces and special characters are included.
    • It removes any trailing newline character added by fgets().
  2. Reading the Character to Remove:
    • The program uses scanf() to read a single character input from the user.
  3. Removing All Occurrences:
    • The program uses a for loop to iterate over the string. It keeps two indices, i for the current character and j for the position in the modified string.
    • If the current character is not the one to be removed, it is copied to the j position, and j is incremented. This effectively skips all occurrences of the specified character.
    • After the loop, a null character (\0) is added to mark the end of the modified string.
  4. Output:
    • The modified string is printed, showing the result after all occurrences of the specified character have been removed.

Recommended Posts