Program to Remove Given Word From the String

Program to Remove Given Word From the String

  • Write a program to Remove Given Word From the String in C
  • Write a program to Remove Given Word From the String in C++
  • Write a program to Remove Given Word From the String in Python
  • Write a program to Remove Given Word From the String in PHP
  • Write a program to Remove Given Word From the String in Java
  • Write a program to Remove Given Word From the String in Java Script
  • Write a program to Remove Given Word From the String in C#

Explanation:

The fundamental method for eliminating a specific word from a string is to look for the word and substitute it with an empty string. To make sure that there are no additional spaces left after deleting the word, we must also manage spaces correctly.

Logic Explanation:

  1. Search for the Word:
    • Look for the word in the string.
    • If the word exists, remove it and ensure there are no extra spaces left between words.
  2. Replace the Word:
    • You can use a loop or string replace function to replace all occurrences of the word with an empty string.
  3. Handle Edge Cases:
    • If the word is at the beginning or end of the string, we should ensure that there are no leading or trailing spaces left.
    • If the word is followed or preceded by punctuation, it should still be removed correctly.
  4. Return the Modified String:
    • After removing the word, return the modified string.

Program to Remove Given Word From the String

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

void removeWord(char *str, const char *word) {
    char temp[200];
    char *pos, *start = str;
    int len = strlen(word);

    temp[0] = '\0';

    while ((pos = strstr(start, word)) != NULL) {
        if ((pos == start || *(pos - 1) == ' ') && (*(pos + len) == ' ' || *(pos + len) == '\0')) {
            strncat(temp, start, pos - start);
            start = pos + len;
        } else {
            strncat(temp, start, pos - start + 1);
            start = pos + 1;
        }
    }

    strcat(temp, start);
    strcpy(str, temp);
}

int main() {
    char str[200], word[50];

    printf("Enter the main string: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';

    printf("Enter the word to remove: ");
    fgets(word, sizeof(word), stdin);
    word[strcspn(word, "\n")] = '\0';

    removeWord(str, word);

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

    return 0;
}

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string removeWord(const string &str, const string &word) {
    istringstream stream(str);
    ostringstream result;
    string token;

    while (stream >> token) {
        if (token != word) {
            if (result.str().length() > 0) {
                result << " ";
            }
            result << token;
        }
    }
    return result.str();
}

int main() {
    string str, word;

    cout << "Enter the main string: ";
    getline(cin, str);

    cout << "Enter the word to remove: ";
    cin >> word;

    string result = removeWord(str, word);
    cout << "Modified string: " << result << endl;

    return 0;
}

def remove_word(string, word):
    words = string.split()
    return ' '.join(w for w in words if w != word)

string = input("Enter the main string: ")
word = input("Enter the word to remove: ")

result = remove_word(string, word)
print("Modified string:", result)

<?php
function removeWord($string, $word) {
    $words = explode(" ", $string);
    $filteredWords = array_filter($words, fn($w) => $w !== $word);
    return implode(" ", $filteredWords);
}

echo "Enter the main string: ";
$string = trim(fgets(STDIN));

echo "Enter the word to remove: ";
$word = trim(fgets(STDIN));

$result = removeWord($string, $word);
echo "Modified string: " . $result . "\n";
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the main string: ");
        String mainString = scanner.nextLine();

        System.out.print("Enter the word to remove: ");
        String word = scanner.next();

        String result = removeWord(mainString, word);
        System.out.println("Modified string: " + result);
    }

    public static String removeWord(String str, String word) {
        String[] words = str.split("\\s+");
        StringBuilder result = new StringBuilder();

        for (String w : words) {
            if (!w.equals(word)) {
                if (result.length() > 0) {
                    result.append(" ");
                }
                result.append(w);
            }
        }
        return result.toString();
    }
}

function removeWord(string, word) {
    return string.split(" ").filter(w => w !== word).join(" ");
}

const string = prompt("Enter the main string:");
const word = prompt("Enter the word to remove:");

const result = removeWord(string, word);
console.log("Modified string:", result);

using System;

class Program {
    static void Main() {
        Console.Write("Enter the main string: ");
        string mainString = Console.ReadLine();

        Console.Write("Enter the word to remove: ");
        string word = Console.ReadLine();

        string result = RemoveWord(mainString, word);
        Console.WriteLine("Modified string: " + result);
    }

    static string RemoveWord(string str, string word) {
        string[] words = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        return string.Join(" ", Array.FindAll(words, w => w != word));
    }
}

List of All Programs