Program to replace a word with asterisks in a sentence | C ++ program to censor a word with asterisks in a sentence ; Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <boost/algorithm/string.hpp> NEW_LINE using namespace std ; string censor ( string text , string word ) { vector < string > word_list ; boost :: split ( word_list , text , boost :: is_any_of ( " \\ ▁ + " ) ) ; string result = " " ; string stars = " " ; for ( int i = 0 ; i < word . size ( ) ; i ++ ) stars += ' * ' ; int index = 0 ; for ( string i : word_list ) { if ( i . compare ( word ) == 0 ) { word_list [ index ] = stars ; } index ++ ; } for ( string i : word_list ) { result += i + ' ▁ ' ; } return result ; } int main ( ) { string extract = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ " " portal ▁ for ▁ geeks . ▁ I ▁ am ▁ pursuing ▁ my ▁ " " major ▁ in ▁ computer ▁ science . ▁ " ; string cen = " computer " ; cout << ( censor ( extract , cen ) ) ; }