For the given sentence as input, censor a specific word with asterisks ‘ * ‘.
Input : word = “computer”
text = “w3wiki is a computer science portal for Beginner. People who love computer and computer codes can contribute their valuables/ideas on computer codes/structures on here.”
Output : w3wiki is a ******** science portal for Beginner. People who love ******** and ******** codes can contribute their valuables/ideas on ******** codes/structures on here.
The idea is to first split given sentence into different words. Then traverse the word list. For every word in the word list, check if it matches with given word. If yes, then replace the word with stars in the list. Finally merge the words of list and print.
C++
#include<bits/stdc++.h>
#include <boost/algorithm/string.hpp>
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 = "w3wiki is a computer science "
"portal for Beginner. I am pursuing my "
"major in computer science. " ;
string cen = "computer" ;
cout << (censor(extract, cen));
}
|
Java
class GFG
{
static String censor(String text,
String word)
{
String[] word_list = text.split( "\\s+" );
String result = "" ;
String stars = "" ;
for ( int i = 0 ; i < word.length(); i++)
stars += '*' ;
int index = 0 ;
for (String i : word_list)
{
if (i.compareTo(word) == 0 )
word_list[index] = stars;
index++;
}
for (String i : word_list)
result += i + ' ' ;
return result;
}
public static void main(String[] args)
{
String extract = "w3wiki is a computer science " +
"portal for Beginner. I am pursuing my " +
"major in computer science. " ;
String cen = "computer" ;
System.out.println(censor(extract, cen));
}
}
|
Python3
def censor(text, word):
word_list = text.split()
result = ''
stars = '*' * len (word)
count = 0
index = 0 ;
for i in word_list:
if i = = word:
word_list[index] = stars
index + = 1
result = ' ' .join(word_list)
return result
if __name__ = = '__main__' :
extract = "w3wiki is a computer science portal for Beginner.\
I am pursuing my major in computer science. "
cen = "computer"
print (censor(extract, cen))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static String censor(String text,
String word)
{
String[] word_list = text.Split( ' ' );
String result = "" ;
String stars = "" ;
for ( int i = 0; i < word.Length; i++)
stars += '*' ;
int index = 0;
foreach (String i in word_list)
{
if (i.CompareTo(word) == 0)
word_list[index] = stars;
index++;
}
foreach (String i in word_list)
result += i + " " ;
return result;
}
public static void Main(String[] args)
{
String extract = "w3wiki is a computer science " +
"portal for Beginner. I am pursuing my " +
"major in computer science. " ;
String cen = "computer" ;
Console.WriteLine(censor(extract, cen));
}
}
|
Javascript
<script>
function censor(text, word) {
var word_list = text.split( " " );
var result = "" ;
var stars = "" ;
for ( var i = 0; i < word.length; i++) stars += "*" ;
var index = 0;
for (const i of word_list) {
if (i === word)
word_list[index] = stars;
index++;
}
for (const i of word_list) {
result += i + " " ;
}
return result;
}
var extract =
"w3wiki is a computer science " +
"portal for Beginner. I am pursuing my " +
"major in computer science. " ;
var cen = "computer" ;
document.write(censor(extract, cen) + "<br>" );
</script>
|
PHP
<?php
function censor( $text , $word )
{
$word_list = explode ( " " , $text );
$result = '' ;
$stars = "" ;
for ( $i = 0; $i < strlen ( $word ); $i ++)
$stars .= "*" ;
$count = 0;
$index = 0;
for ( $i = 0; $i < sizeof( $word_list ); $i ++)
{
if ( $word_list [ $i ] == $word )
$word_list [ $index ] = $stars ;
$index += 1;
}
return implode( ' ' , $word_list );
}
$extract = "w3wiki is a computer science " .
"portal for Beginner.\nI am pursuing my " .
"major in computer science. " ;
$cen = "computer" ;
echo censor( $extract , $cen );
?>
|
Output
w3wiki is a ******** science portal for Beginner. I am pursuing my major in ******** science.
- Time complexity: O(length(word)+M), where M is the number of words in text
- Auxiliary Space: O(1)
Approach : Using replace() in python3.
Replace() method searches for the string passed as the first argument in the given string and then replaces that with the second argument.
C++
#include <iostream>
#include <string>
int main()
{
std::string extract
= "w3wiki is a computer science portal for "
"Beginner. I am pursuing my major in computer "
"science." ;
std::string cen = "computer" ;
size_t found = extract.find(cen);
while (found != std::string::npos) {
extract.replace(found, cen.length(),
std::string(cen.length(), '*' ));
found = extract.find(cen, found + cen.length());
}
std::cout << extract << std::endl;
return 0;
}
|
Java
public class CensorWordInSentence {
public static void main(String[] args)
{
String extract
= "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science." ;
String cen = "computer" ;
extract = extract.replace(cen,
"*" .repeat(cen.length()));
System.out.println(extract);
}
}
|
Python3
extract = "w3wiki is a computer science portal for Beginner.I am pursuing my major in computer science. "
cen = "computer"
extract = extract.replace(cen, '*' * len (cen))
print (extract)
|
C#
using System;
class Program {
static void Main()
{
string extract
= "w3wiki is a computer science portal for "
+ "Beginner. I am pursuing my major in computer "
+ "science." ;
string cen = "computer" ;
int found = extract.IndexOf(
cen, StringComparison.OrdinalIgnoreCase);
while (found != -1) {
extract
= extract.Remove(found, cen.Length)
.Insert(found,
new string ( '*' , cen.Length));
found = extract.IndexOf(
cen, found + cen.Length,
StringComparison.OrdinalIgnoreCase);
}
Console.WriteLine(extract);
}
}
|
Javascript
let extract = "w3wiki is a computer science portal for Beginner.I am pursuing my major in computer science." ;
let cen = "computer" ;
extract = extract.replace( new RegExp(cen, 'gi' ), '*' .repeat(cen.length));
console.log(extract);
|
Output
w3wiki is a ******** science portal for Beginner.\I am pursuing my major in ******** science.
Using re module:
You can use the re module in Python to search for the specific word in the sentence and replace it with the asterisks.
The censor function takes two parameters, text and word.
It searches for all occurrences of word in text using a regular expression with the global g flag, and replaces each occurrence with asterisks of the same length using the replace function.
C++
#include <iostream>
#include <regex>
std::string censor( const std::string& text,
const std::string& word)
{
std::regex pattern(word);
std::string result = std::regex_replace(
text, pattern, std::string(word.length(), '*' ));
return result;
}
int main()
{
std::string text
= "w3wiki is a computer science portal for "
"Beginner. I am pursuing my major in computer "
"science." ;
std::string word = "computer" ;
std::cout << censor(text, word) << std::endl;
return 0;
}
|
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static String censor(String text, String word)
{
Pattern pattern = Pattern.compile(word);
Matcher matcher = pattern.matcher(text);
String result
= matcher.replaceAll( "*" .repeat(word.length()));
return result;
}
public static void main(String[] args)
{
String text
= "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science." ;
String word = "computer" ;
System.out.println(censor(text, word));
}
}
|
Python3
import re
def censor(text, word):
result = re.sub(word, '*' * len (word), text)
return result
text = "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science."
word = "computer"
print (censor(text, word))
|
C#
using System;
using System.Text.RegularExpressions;
class MainClass {
public static string Censor( string text, string word) {
Regex regex = new Regex(Regex.Escape(word));
string result = regex.Replace(text, new string ( '*' , word.Length));
return result;
}
public static void Main ( string [] args) {
string text = "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science." ;
string word = "computer" ;
Console.WriteLine(Censor(text, word));
}
}
|
Javascript
function censor(text, word) {
const regex = new RegExp(word, 'gi' );
const result = text.replace(regex, '*' .repeat(word.length));
return result;
}
const text = "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science." ;
const word = "computer" ;
console.log(censor(text, word));
|
Output
w3wiki is a ******** science portal for Beginner. I am pursuing my major in ******** science.
This approach has a time complexity of O(N), where N is the length of the text. The auxiliary space required is O(1).
Using regular expression
In the regular expression, we pass the word variable as the pattern to search for. The g flag means that it will find all occurrences of the pattern, not just the first one.
In the replace function, we pass a callback function that returns an asterisk (*) repeated for the length of the matched string.
This ensures that the asterisks have the same length as the word that was replaced.
C++
#include <bits/stdc++.h>
using namespace std;
string censor(string text, string word)
{
regex r(word);
string result = regex_replace(text, r, string(word.length(), '*' ));
return result;
}
int main() {
string text = "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science." ;
string word = "computer" ;
cout << censor(text, word) <<endl;
return 0;
}
|
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CensorText {
public static String censor(String text, String word) {
Pattern pattern = Pattern.compile( "\\b" + word + "\\b" );
Matcher matcher = pattern.matcher(text);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
String replacement = new String( new char [word.length()]).replace( '\0' , '*' );
matcher.appendReplacement(result, replacement);
}
matcher.appendTail(result);
return result.toString();
}
public static void main(String[] args) {
String text = "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science." ;
String word = "computer" ;
System.out.println(censor(text, word));
}
}
|
Python3
def censor(text, word):
result = text.replace(word, "*" * len (word))
return result
text = "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science."
word = "computer"
print (censor(text, word))
|
C#
using System;
using System.Text.RegularExpressions;
class Program
{
static string Censor( string text, string word)
{
string result = Regex.Replace(text, word, new string ( '*' , word.Length));
return result;
}
static void Main()
{
string text = "w3wiki is a computer science portal for Beginner.I am pursuing my major in computer science." ;
string word = "computer" ;
Console.WriteLine(Censor(text, word));
}
}
|
Javascript
function censor(text, word) {
const regex = new RegExp(word, "g" );
const result = text.replace(regex, "*" .repeat(word.length));
return result;
}
const text = "w3wiki is a computer science portal for Beginner. I am pursuing my major in computer science." ;
const word = "computer" ;
console.log(censor(text, word));
|
Output
w3wiki is a ******** science portal for Beginner. I am pursuing my major in ******** science.
Time complexity: O(n), where n is the length of the input text string.