JulianJulianov

04.TextProcessingLab-Text Filter

Apr 15th, 2020
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. 04. Text Filter
  2. Write a program that takes a text and a string of banned words. All words included in the ban list should be replaced with
  3. asterisks "*", equal to the word's length. The entries in the ban list will be separated by a comma and space ", ".
  4. The ban list should be entered on the first input line and the text on the second input line.
  5.  
  6. Examples
  7.  
  8. Input                                                                 Output
  9. Linux, Windows                                                        It is not *****, it is GNU/*****. ***** is merely the kernel,
  10. It is not Linux, it is GNU/Linux. Linux is merely the kernel,         while GNU adds the functionality. Therefore we owe it to them
  11. while GNU adds the functionality. Therefore we owe it to them         by calling the OS GNU/*****! Sincerely, a ******* client
  12. by calling the OS GNU/Linux! Sincerely, a Windows client     
  13. Hints
  14. Read the input.
  15. Replace all ban words in the text with asterisk (*).
  16. Use the built-in method Replace(banWord, replacement).
  17. Use new string(char ch, int repeatCount) to create the replacement
  18.  
  19. using System;
  20.  
  21. public class Program
  22. {
  23.     public static void Main()
  24.     {
  25.         var bannedWords = Console.ReadLine().Split(", ");
  26.        var text = Console.ReadLine();
  27.        
  28.        foreach (var item in bannedWords)
  29.        {
  30.            var numAsterisks = "*";
  31.            var asterisks = "";
  32.            
  33.            for (int i = 1; i <= item.Length; i++)
  34.            {
  35.                asterisks += numAsterisks;
  36.            }
  37.            text = text.Replace(item, asterisks);
  38.        }
  39.        Console.WriteLine($"{text}");
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment