Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace _03.WordEncounter
- {
- public class WordEncounter
- {
- public static void Main()
- {
- var words = new List<string>();
- var filter = Console.ReadLine();
- var symbol = filter[0];
- var digitString = string.Empty;
- for (int i = 1; i < filter.Length; i++)
- {
- digitString += filter[i];
- }
- var number = int.Parse(digitString);
- var listOfSentences = Console.ReadLine();
- var pattern = @"[A-Z](.*?)(\.|\?|\!)";
- var regex = new Regex(pattern);
- while (true)
- {
- if (listOfSentences == "end")
- {
- break;
- }
- var isValidSentence = regex.IsMatch(listOfSentences);
- if (isValidSentence)
- {
- var matches = regex.Matches(listOfSentences);
- foreach (Match match in matches)
- {
- var sentenceToString = match.Value;
- var wordsInSentence = sentenceToString.Split(new char[] { ' ', ',', '.', '!', '?', ';', ':', '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var word in wordsInSentence)
- {
- var count = 0;
- for (int i = 0; i < word.Length; i++)
- {
- if (word[i] == symbol)
- {
- count++;
- }
- }
- if (count == number)
- {
- words.Add(word);
- }
- }
- }
- }
- listOfSentences = Console.ReadLine();
- }
- Console.WriteLine(string.Join(", ", words));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement