Removing specific words from a string input

remove word from string python
how to remove a word from a string in c++
how to remove a word from a sentence in java
remove a particular word from a string in c
remove substring from string c++
pandas remove words from string
remove word from string javascript
remove the occurrence of the word "the" from the entered string

In the required code , I have to input a string and output that string after removing all occurences of a given word in that string. Example: Say I wish to remove WUB from the string.





Here is my code:

#include <bits/stdc++.h>

using namespace std;

int main()
    string in;
    cin>>in;                           //input
    int n=in.length();

    int pos =in.find("WUB");
            pos =in.find("WUB");
        if(pos!=-1){                //manipulating string by erasing and inserting spaces between words
            in.insert(pos+3," ");
            if(in[0]==' ')
            if(in[n-1]==' ')                      //removing extra spaces

It works for input 1, but for input 2 gives wrong output. Output2:WEETHEONSND

What is wrong here?

The overload of string::erase you use is

basic_string& erase( size_type index = 0, size_type count = npos );

You are passing pos+3 as count, instead of just 3.

Remove a specific word from a string, String regex = "\\s*\\bis\\b\\s*"; content = content. white space on either side of the word being removed (if you want to remove this too). c)); } public static String removeAllOccurrencesOfChar(String input, char c) { String r� Input: String = "Geeks For Geeks", Word = "For" Output: "Geeks Geeks" Input: String = "A computer Science Portal", Word = "Geeks" Output: "A computer Science Portal" Approach : In Java, this can be done using String replaceAll method by replacing given word with a blank space. Below is the solution to the above problem:

Instead of two sequential calls of the function insert and erase you could use only one call of the function replace.

Moreover this call of member function earse


The second argument shall specify the number of characters that need to be erased.

Also it seems you need to remove all leading and trailing blanks instead of only one blank from the both sides.

        if(in[0]==' ')
        if(in[n-1]==' ')                      //removing extra spaces

The program can look the following way

#include <iostream>
#include <string>
#include <limits>

int main() 
    while ( true )
        std::cout << "Enter the source string (Enter - exit): ";

        std::string s;

        if ( not std::getline( std::cin, s ) or s.empty() ) break;

        std::cout << "Enter the substring to remove: ";

        std::string t;

        std::cin >> t;
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

        if ( not t.empty() )
            for ( auto pos = s.find( t ); 
                  pos != std::string::npos;
                  pos = s.find( t, pos ) )
                s.replace( pos, t.size(), 1, ' ' );

        s.erase( 0, s.find_first_not_of( " \t " ) );
        auto pos = s.find_last_not_of( " \t " ); 
        if ( pos != std::string::npos ) s.erase( ++pos );

        std::cout << "The source string: \"" << s << "\"\n";

    return 0;

Its output might look like

Enter the source string (Enter - exit): WUBWUBABCWUB
Enter the substring to remove: WUB
The source string: "ABC"
Enter the substring to remove: WUB
The source string: "WE ARE  THE CHAMPIONS MY FRIEND"
Enter the source string (Enter - exit): 

Removing list of words from a string, I noticed that you want to also remove a word if its lower-case variant is in the list, If the input query is "What is Hello", I get the output as:. Removing specific words from a string input. Ask Question Asked 26 days ago. Active 26 days ago. Viewed 66 times 0. In the required code , I have to input a string

You can create more efficient and neat code by using Regex to find the unwanted words, and then replace them with a space. Of course you will then have to use unique() to remove the double spaces.


#include <iostream>
#include <regex>

bool BothAreSpaces(char lhs, char rhs)
    return (lhs == rhs) && (lhs == ' ');

int main()
    regex e("(WUB)");   // Regex syntax to filtr unwanted string
    string result; //Filtered string

    regex_replace(std::back_inserter(result), s.begin(), s.end(), e, " "); //Replace unwanted word with space
    string::iterator new_end = unique(result.begin(), result.end(), BothAreSpaces); //Find double spaces
    result.erase(new_end, result.end()); //Remove double spaces
    cout << result << endl;



How to delete substring from original string in Java, How can I remove duplicate lines which is starting with a specific string from a file in Java? 791 Views � How do I String input = scan.nextLine(); Do substring from 0 to the first index and last index of the word to last index of the actual string. 1) Split input sentence separated by space into words. 2) So to get all those strings together first we will join each string in given list of strings. 3) Now create a dictionary using Counter method having strings as keys and their frequencies as values. 4) Join each words are unique to form single string.

Python Remove Character from String, Let's see how to remove a word from a string. s = 'ab12abc34ba' print(s.replace(' ab', '')). The TRANWRD function differs from the TRANSTRN function because TRANSTRN allows the replacement string to have a length of zero. TRANWRD uses a single blank instead when the replacement string has a length of zero. The TRANSLATE function converts every occurrence of a user-supplied character to another character.

Java Remove Character from String, I want to replace a few words from a String , Whenever a match will be founded it will remove that match . Example : “Learning java is not so easy but also” /* is not � We can also insert a string at a specified positon in another string. String.Insert method inserts a specified string at a specified index position in an instance. For example, the following code inserts "bbb" after the second character in str1 and the result string is "pbbbpp".

C Program to Remove given Word from a String, 1. Take a string and its substring as input. 2. Put each word of the input string into the rows of 2-D array. 3� Remember however, your sentence may contains the same word twice. So if you want to find the second one, you should process the string by removing word entries from the sentence as you walk trough it string str = "Hello this is a sentence."; string sentence = "Hello this is a sentence.

  • try using the debugger are probably messing up with string length and index
  • 1) Don't #include <bits/stdc++.h> 2) Properly format your code, if not for yourself then for us. 3) As soon as in contains one "WUB", in[n-1] will read outside the string.
  • You can write the pos = ... at the end of the loop instead of the begin and remove if(pos!=-1), by the way. Plus, for(;pos!=-1;){ is just a strange way to write while(pos!=-1).
  • Why are you not simply writing s=std::regex_replace(s,std::regex("WUB","")?
  • I followed the regex documentation on cppreference, but yours works as well :)