Advertisement
Danny_Berova

02.WordEncounter -fixed

Aug 11th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace _02.WordEncounter
  9. {
  10.     class WordEncounter
  11.     {
  12.         static void Main()
  13.         {
  14.             Regex words = new Regex(@"\w+");
  15.             List<string> validWords = new List<string>();
  16.  
  17.             string input = Console.ReadLine();
  18.  
  19.             char filter = input[0];
  20.             int letterCount = int.Parse(input[1].ToString());
  21.  
  22.             string sentences = Console.ReadLine();
  23.  
  24.             //bool isValid = char.IsUpper(sentences[0]) && (sentences.EndsWith(".") || sentences.EndsWith("!") || sentences.EndsWith("?"));
  25.  
  26.             while (sentences != "end")
  27.             {
  28.                 string sentencePattern = @"^[A-Z].*[.?!]$";
  29.                 Regex validSentence = new Regex(sentencePattern);
  30.  
  31.                 if (validSentence.IsMatch(sentences)) // chanded statement
  32.                 {
  33.                     MatchCollection matches = words.Matches(sentences);
  34.  
  35.                     foreach (Match match in matches)
  36.                     {
  37.                         int cntr = 0;
  38.                         foreach (char @char in match.Value)
  39.                         {
  40.                             if (@char == filter)
  41.                             {
  42.                                 cntr++;
  43.                             }
  44.                         }
  45.                         if (cntr >= letterCount)
  46.                         {
  47.                             validWords.Add(match.Value);
  48.                         }
  49.                     }
  50.                 }
  51.  
  52.                 sentences = Console.ReadLine();
  53.             }
  54.             Console.WriteLine(string.Join(", ", validWords));
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement