对于给定的句子作为输入,检查带有星号’ *
‘的特定单词。
例子 :
Input : word = “computer”
text = “GeeksforGeeks 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 : GeeksforGeeks is a ******** science portal for geeks. People who love ******** and ******** codes can contribute their valuables/ideas on ******** codes/structures on here.
想法是首先将给定的句子拆分为不同的单词。然后遍历单词列表。对于单词列表中的每个单词,请检查其是否与给定单词匹配。如果是,则将单词替换为列表中的星号。最后,合并列表中的单词并打印。
C++
// C++ program to censor a word
// with asterisks in a sentence
#include
#include
using namespace std;
// Function takes two parameter
string censor(string text,
string word)
{
// Break down sentence by ' ' spaces
// and store each individual word in
// a different list
vector word_list;
boost::split(word_list, text, boost::is_any_of("\\ +"));
// A new string to store the result
string result = "";
// Creating the censor which is an asterisks
// "*" text of the length of censor word
string stars = "";
for (int i = 0; i < word.size(); i++)
stars += '*';
// Iterating through our list
// of extracted words
int index = 0;
for (string i : word_list)
{
if (i.compare(word) == 0)
{
// changing the censored word to
// created asterisks censor
word_list[index] = stars;
}
index++;
}
// join the words
for (string i : word_list)
{
result += i + ' ';
}
return result;
}
// Driver code
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));
}
// This code is contributed by Rajput-Ji
Java
// Java program to censor a word
// with asterisks in a sentence
class GFG
{
// Function takes two parameter
static String censor(String text,
String word)
{
// Break down sentence by ' ' spaces
// and store each individual word in
// a different list
String[] word_list = text.split("\\s+");
// A new string to store the result
String result = "";
// Creating the censor which is an asterisks
// "*" text of the length of censor word
String stars = "";
for (int i = 0; i < word.length(); i++)
stars += '*';
// Iterating through our list
// of extracted words
int index = 0;
for (String i : word_list)
{
if (i.compareTo(word) == 0)
// changing the censored word to
// created asterisks censor
word_list[index] = stars;
index++;
}
// join the words
for (String i : word_list)
result += i + ' ';
return result;
}
// Driver code
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));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python Program to censor a word
# with asterisks in a sentence
# Function takes two parameter
def censor(text, word):
# Break down sentence by ' ' spaces
# and store each individual word in
# a different list
word_list = text.split()
# A new string to store the result
result = ''
# Creating the censor which is an asterisks
# "*" text of the length of censor word
stars = '*' * len(word)
# count variable to
# access our word_list
count = 0
# Iterating through our list
# of extracted words
index = 0;
for i in word_list:
if i == word:
# changing the censored word to
# created asterisks censor
word_list[index] = stars
index += 1
# join the words
result =' '.join(word_list)
return result
# Driver code
if __name__== '__main__':
extract = "GeeksforGeeks is a computer science portal for geeks.\
I am pursuing my major in computer science. "
cen = "computer"
print(censor(extract, cen))
C#
// C# program to censor a word
// with asterisks in a sentence
using System;
using System.Collections.Generic;
class GFG
{
// Function takes two parameter
static String censor(String text,
String word)
{
// Break down sentence by ' ' spaces
// and store each individual word in
// a different list
String[] word_list = text.Split(' ');
// A new string to store the result
String result = "";
// Creating the censor which is an asterisks
// "*" text of the length of censor word
String stars = "";
for (int i = 0; i < word.Length; i++)
stars += '*';
// Iterating through our list
// of extracted words
int index = 0;
foreach (String i in word_list)
{
if (i.CompareTo(word) == 0)
// changing the censored word to
// created asterisks censor
word_list[index] = stars;
index++;
}
// join the words
foreach (String i in word_list)
result += i + " ";
return result;
}
// Driver code
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";
Console.WriteLine(censor(extract, cen));
}
}
// This code is contributed by PrinciRaj1992
PHP
输出 :
GeeksforGeeks is a ******** science portal for geeks.
I am pursuing my major in ******** science.