C program to remove first occurrence of a character from string

Category: C Program

Learn how to write a C program to remove the first occurrence 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 the first occurrence of a specified character from a given string. This operation is common in text processing tasks such as editing, parsing, and data cleaning.

Objective

The goal is to create a C program that takes a string and a character as inputs, finds the first occurrence of the specified character in the string, removes it, 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. Find the first occurrence of the specified character in the string.
  4. Remove the character by shifting subsequent characters to the left.
  5. 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. Find the first occurrence of the character:
    • Iterate over the string to find the index of the first occurrence of the specified character.
  4. Remove the character:
    • Shift the characters following the found index to the left to overwrite the character.
  5. Print the modified string:
    • Print the resulting string after the character has been removed.

Write a C program to remove first occurrence 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 index = -1;

    // 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);

    // Find the first occurrence of the character
    for (int i = 0; i < strlen(str); i++) {
        if (str[i] == char_to_remove) {
            index = i;
            break;
        }
    }

    // If the character is found, remove it by shifting the subsequent characters
    if (index != -1) {
        for (int i = index; i < strlen(str); i++) {
            str[i] = str[i + 1];
        }
    }

    // Print the modified string
    if (index != -1) {
        printf("String after removing first occurrence of '%c': %s", char_to_remove, str);
    } else {
        printf("Character '%c' not found in the string.", char_to_remove);
    }

    return 0;
}

Output

Enter a string: learn programming at ProCoding
Enter the character to remove: p
String after removing first occurrence of 'p': learn rogramming at 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. Finding the First Occurrence:
    • A for loop iterates over the string to find the first index where the specified character occurs. If found, the index is stored in the variable index.
  4. Removing the Character:
    • If the character is found (index != -1), another loop shifts all characters after the found index to the left by one position, effectively removing the character.
  5. Output:
    • The modified string is printed if the character was found and removed. If the character was not found, a message is printed to inform the user.

Recommended Posts