Program to replace a word with asterisks in a sentence | Java 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
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 = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ " + " portal ▁ for ▁ geeks . ▁ I ▁ am ▁ pursuing ▁ my ▁ " + " major ▁ in ▁ computer ▁ science . ▁ " ; String cen = " computer " ; System . out . println ( censor ( extract , cen ) ) ; } }