C program to remove first occurrence of a word from string

Category: C Program

Learn how to write a C program to remove the first occurrence 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 common task in programming, and one such task is removing the first occurrence of a specific word from a string. This article will guide you through creating a C program to accomplish this using both manual string manipulation and the strstr function.

Introduction

In this article, we will write a C program that removes the first occurrence of a word from a given string. We will use basic string manipulation techniques and also show how to achieve this using the strstr function. The program will take a string and a word as input, search for the first occurrence of the word in the string, and then remove it.

Steps to Solve the Problem

  1. Input the String and Word: Read the input string and the word to be removed.
  2. Search for the Word: Find the first occurrence of the word in the string.
  3. Remove the Word: Modify the string to remove the word.
  4. Print the Result: Output the modified string.

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

Here is the C program to remove the first occurrence of a word from a given string without using the strstr function:

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

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

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

    // Search for the first occurrence of the word in the string
    for (i = 0; i <= strLen - wordLen; i++) {
        for (j = 0; j < wordLen; j++) {
            if (str[i + j] != word[j]) {
                break;
            }
        }

        // If the word is found
        if (j == wordLen) {
            found = 1;

            // Shift the remaining characters to the left
            for (k = i; k <= strLen - wordLen; k++) {
                str[k] = str[k + wordLen];
            }
            str[strLen - wordLen] = '\0'; // Terminate the string
            break;
        }
    }

    if (found) {
        printf("Modified string: %s", str);
    } else {
        printf("Word not found in the string.");
    }

    return 0;
}

Output

Enter the string: time and time again, the rain keeps falling down
Enter the word to remove: rain
Modified string: time and time again, the  keeps falling down

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 for the Word: We use nested loops to search for the first occurrence of the word in the string.
    • The outer loop iterates over each position in the string where the word could start.
    • The inner loop checks if the word matches the substring starting at the current position.
  3. Remove the Word:
    • If a match is found, we shift the characters to the left starting from the position after the word to the end of the string.
    • This effectively removes the word from the string.
  4. Print the Result: We print the modified string. If the word is not found, we print a message indicating that the word was not found.

Implementation Using strstr

Here is the C program to remove the first occurrence 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 the position of the first occurrence of the word
    pos = strstr(str, word);

    if (pos != NULL) {
        // Calculate lengths
        int len = strlen(word);
        int i;

        // Remove the word by shifting the remaining characters to the left
        for (i = 0; pos[i + len] != '\0'; i++) {
            pos[i] = pos[i + len];
        }
        pos[i] = '\0'; // Terminate the string

        printf("Modified string: %s\n", str);
    } else {
        printf("Word not found in the string.\n");
    }

    return 0;
}

Explanation of strstr

The strstr function finds the first occurrence of a substring in a string. It returns a pointer to the first occurrence of the substring, or NULL if the substring is not found. By using this pointer, we can easily remove the word from the string by shifting the remaining characters to the left.


Recommended Posts