Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 04. Text Filter
- Write a program that takes a text and a string of banned words. All words included in the ban list should be replaced with
- asterisks "*", equal to the word's length. The entries in the ban list will be separated by a comma and space ", ".
- The ban list should be entered on the first input line and the text on the second input line.
- Examples
- Input Output
- Linux, Windows It is not *****, it is GNU/*****. ***** is merely the kernel,
- It is not Linux, it is GNU/Linux. Linux is merely the kernel, while GNU adds the functionality. Therefore we owe it to them
- while GNU adds the functionality. Therefore we owe it to them by calling the OS GNU/*****! Sincerely, a ******* client
- by calling the OS GNU/Linux! Sincerely, a Windows client
- Hints
- Read the input.
- Replace all ban words in the text with asterisk (*).
- Use the built-in method Replace(banWord, replacement).
- Use new string(char ch, int repeatCount) to create the replacement
- using System;
- public class Program
- {
- public static void Main()
- {
- var bannedWords = Console.ReadLine().Split(", ");
- var text = Console.ReadLine();
- foreach (var item in bannedWords)
- {
- var numAsterisks = "*";
- var asterisks = "";
- for (int i = 1; i <= item.Length; i++)
- {
- asterisks += numAsterisks;
- }
- text = text.Replace(item, asterisks);
- }
- Console.WriteLine($"{text}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment