C program to remove all occurrence of a word from string

Category: C Program

Learn how to write a C program to remove all occurrences of a word from a string. This tutorial includes step-by-step instructions, code implementation without and with the strstr function, and explanations to help you understand the process of removing a word from a string using basic string manipulation techniques.

String manipulation is a key aspect of programming in C. One common task is to remove all occurrences of a specific word from a string. This article will guide you through writing a C program to achieve this task using basic string manipulation techniques. We will first implement the logic manually without using strstr, and then we will use the strstr function to achieve the same result.

Steps to Solve the Problem

  1. Input the String and Word: Read the input string and the word to be removed.
  2. Search for All Occurrences of the Word: Find and remove all occurrences of the word in the string.
  3. Print the Result: Output the modified string.

Write a C program to remove all occurrence of a word from string

Here is the C program to remove all occurrences of a word from a given string:

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

int main() {
    char str[200], word[20];
    int i, j, k;
    int strLen, wordLen;

    // Input the string
    printf("Enter the string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0'; // Removing trailing newline character

    // Input the word to be removed
    printf("Enter the word to remove: ");
    scanf("%s", word);

    strLen = strlen(str);
    wordLen = strlen(word);

    for (i = 0; i <= strLen - wordLen;) {
        // Check if the word matches
        for (j = 0; j < wordLen; j++) {
            if (str[i + j] != word[j]) {
                break;
            }
        }

        // If the word matches, remove it
        if (j == wordLen) {
            for (k = i; k <= strLen - wordLen; k++) {
                str[k] = str[k + wordLen];
            }
            strLen -= wordLen;
            str[strLen] = '\0'; // Update the string length and terminate the string
        } else {
            i++;
        }
    }

    printf("Modified string: %s", str);

    return 0;
}

Output

Enter the string: The fog grew thicker and thicker, obscuring the path ahead
Enter the word to remove: thicker
Modified string: The fog grew  and , obscuring the path ahead

Explanation of the Code

  1. Input Handling: We read the input string using fgets and remove the trailing newline character. We then read the word to be removed using scanf.
  2. Search and Remove All Occurrences:
    • We use a loop to iterate over each position in the string where the word could start.
    • We use a nested loop to check if the word matches the substring starting at the current position.
    • If a match is found, we shift the characters to the left to remove the word and update the string length.
    • If no match is found, we move to the next position.
  3. Print the Result: We print the modified string.

Implementation Using strstr

Here is the C program to remove all occurrences of a word from a given string using the strstr function:

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

int main() {
    char str[200], word[20];
    char *pos;

    // Input the string
    printf("Enter the string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0'; // Removing trailing newline character

    // Input the word to be removed
    printf("Enter the word to remove: ");
    scanf("%s", word);

    // Find and remove all occurrences of the word
    while ((pos = strstr(str, word)) != NULL) {
        int len = strlen(word);
        memmove(pos, pos + len, strlen(pos + len) + 1);
    }

    printf("Modified string: %s", str);

    return 0;
}

Explanation of the Code

  1. Input Handling: Similar to the previous implementation, we read the input string and the word to be removed.
  2. Search and Remove All Occurrences:
    • We use a loop to find each occurrence of the word using strstr.
    • If a match is found, we use memmove to shift the characters to the left and remove the word.
    • We continue the loop until no more occurrences of the word are found.
  3. Print the Result: We print the modified string.

Recommended Posts