Change language

Program to replace a word with asterisks in a sentence

Example:

Input: word = “computer”
text = “Python.Engineering is a computer science portal for geeks. People who love computer and computer codes can contribute their valuables / ideas on computer codes / structures on here. ”
Output: Python.Engineering is a ******** science portal for geeks ... People who love ******** and ******** codes can contribute their valuables / ideas on ******** codes / structures on here.

The idea is to break the given sentence into different words first. Then go through the word list. For each word in the word list, check if it matches the given word. If so, replace the word with asterisks in the list. Finally, concatenate the list words and type.

Java

// Java program for censoring a word
// with asterisks in the sentence

class GFG

{

  
// The function takes two parameters

static String censor (String text, 

String word) 

{

 

// Splitting the sentence by spaces

  // and store each individual word in

  // another list

String [] word_list = text.split ( " s +" );

 

// New line to save the result

  String result = "" ;

 

// Create an asterisk censor

  // & quot; * & quot; censored word length text

String stars = "" ;

for ( int i = 0 ; i "word.length (); i ++)

stars + = ’*’ ;

 

// iterate over our list

  // extracted words

int index = 0 ;

for (String i: word_list ) 

{

  if (i.compareTo (word) == 0 )

 

  // change the censored word to

  // asterisk censor created

word_list [index] = stars;

index ++;

}

 

  // join words

for (String i: word_list)

result + = i + ’’ ;

 

return result;

}

 
// Driver code

public static void main (String [] args) 

{

String extract = "Python.Engineering is a computer science" +

"portal for geeks. I am pursuing my " +

  " major in computer science. " ;

String cen = "computer" ;

System.out.println (censor (extract, cen));

}
}

 
// This code is provided
// sanjeev2552

python3

# Python program to censor the word
# with asterisks in the sentence

 

 
# The function takes two parameters

def censor (text, word):

 

# Split sentence into spaces

  # and store each individual word in

  # another list

word_list = text.split ()

  

  # New line to save the result

result = ’’

 

# Create an asterisk censor

# & quot; * & quot; censored word length text

stars = ’*’ * len (word)

  

  # count variable for

# access to our word_list

count = 0

 

# Iterate through our list

Number of words extracted

index = 0 ;

for i in word_list:

 

if i = = word:

 

# change the censored word to

# asterisk-created censor

word_list [index] = stars

  index + = 1

 

  # join words

result = ’’ . join (word_list)

 

return result

 
# Driver code

if __ name__ = = ’ __main__’ :

 

  extract = "Geek sforGeeks is a computer science portal for geeks.

I am pursuing my major in computer science. " 

  cen = "computer"  

print (censor (extract, cen))

C #

PHP

// C # program for censoring words
// with asterisks in the sentence

using System;

using System.Collections.Generic;

 

class GFG

{

 
// The function accepts two parameters

static String censor (String text, 

String word) 

{

 

// Splitting space suggestion

// and store each individual word in

// another list

String [] word_list = text.Split ( ’’ );

 

// New line to save the result

  String result = "" ;

 

// Create an asterisk censor

  // & quot; * & quot; censored word length text

String stars = "" ;

for ( int i = 0; i "word.Length; i ++)

  stars + = ’ * ’ ;

 

// iterate over our list

  // extracted words

int index = 0;

foreach (String i in word_list) 

{

if (i.CompareTo (word) == 0)

  

  // change the censored word to

  // asterisk censor created

word_list [index] = stars;

index ++;

}

 

  // join words

foreach (String i in word_list)

result + = i + "" ;

 

return result;

}

 
// Driver code

public static void Main (String [] args) 

{

String extract = "Python.Engineering is a computer science" +

"portal for geeks. I am pursuing my " +

  " major in computer science. " ;

String cen = "computer" ;

Console.WriteLine (censor (extract, cen));

}
}

 
// This code is provided by PrinciRaj1992

& lt;? Php 
/ / PHP program for censoring a word
// with asterisks in the sentence

 
// The function takes two parameters

function censor ( $ text , $ word )

{

 

// Split the sentence by spaces

// and save every single word in

// another list

$ word_list = explode ( "" , $ text ); 

 

// New line to save the result

  $ result = ’’ ;

 

// Create a censor that is

  // asterisks & quot; * & quot; text length

// censor word

$ stars = "" ;

for ( $ i = 0; $ i " strlen ( $ word ); $ i ++)

$ stars . = "*" ;

 

// count the variable to access

  // our wordlist

$ count = 0;

 

// iterate over our list

  // extracted words

$ index = 0;

for ( $ i = 0; $ i "sizeof ( $ word_list ); $ i ++)

{

if ( $ word_list [ $ i ] == $ word )

 

// change the censored word to

// created by Asterisk Enhancer

$ word_list [ $ index ] = $ stars ;

$ index + = 1;

}

 

  // join words

return implode ( ’’ , $ word_list );

}

 
// Driver code

$ extract = "Python.Engineering is a computer science" .

"portal for geeks. I am pursuing my" .

"major in computer science."

$ cen = "computer" ;

echo censor ( $ extract , $ cen );

 
// This code has been added
// Aman Ojha
?"


Output:

 Python.Engineering is a ******** science portal for geeks. I am pursuing my major in ******** science. 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method