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

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically