public static void Main() { var privateMessagePattern = @"^(\d+)\s<->\s([\d\w]+)$"; var broadcastPattern = @"^(\D+)\s<->\s([\d\w]+)$"; Regex privateMessageRegex = new Regex(privateMessagePattern); Regex broadcastRegex = new Regex(broadcastPattern); var privateMessagesSummary = new List <PrivateMessage>(); var broadcastsSummmary = new List <Broadcast>(); var commData = Console.ReadLine(); while (commData != "Hornet is Green") { var privateMessageMatch = privateMessageRegex.Match(commData); var broadcastMatch = broadcastRegex.Match(commData); if (privateMessageRegex.IsMatch(commData)) { var recipientArr = privateMessageMatch.Groups[1].Value.Reverse().ToArray(); var recipient = string.Join("", recipientArr); var message = privateMessageMatch.Groups[2].Value; var currentPrivateMessage = new PrivateMessage() { Recipient = recipient, Message = message }; privateMessagesSummary.Add(currentPrivateMessage); } else if (broadcastRegex.IsMatch(commData)) { var message = broadcastMatch.Groups[1].Value; var frequency = broadcastMatch.Groups[2].Value; var sb = new StringBuilder(); for (int i = 0; i < frequency.Length; i++) { if ((int)frequency[i] >= 65 && (int)frequency[i] <= 90) { sb.Append((char)(frequency[i] + 32)); } else if ((int)frequency[i] >= 97 && (int)frequency[i] <= 122) { sb.Append((char)(frequency[i] - 32)); } else { sb.Append(frequency[i]); } } var changedBroadcast = sb.ToString(); var currentBroadcast = new Broadcast() { Frequency = changedBroadcast, Message = message }; broadcastsSummmary.Add(currentBroadcast); } commData = Console.ReadLine(); } Console.WriteLine($"Broadcasts:"); if (broadcastsSummmary.Count == 0) { Console.WriteLine("None"); } foreach (var broadcast in broadcastsSummmary) { Console.WriteLine($"{broadcast.Frequency} -> {broadcast.Message}"); } Console.WriteLine($"Messages:"); if (privateMessagesSummary.Count == 0) { Console.WriteLine("None"); } foreach (var privateMessage in privateMessagesSummary) { Console.WriteLine($"{privateMessage.Recipient} -> {privateMessage.Message}"); } }
static void Main(string[] args) { var privites = new List <Privite>(); var broadcastes = new List <Broadcast>(); var line = Console.ReadLine(); while (line != "Hornet is Green") { var splited = line .Split(new string[] { " <-> " }, StringSplitOptions.RemoveEmptyEntries) .ToList(); if (splited.Count >= 2) { var firstQuery = splited[0]; var secondQuery = splited[1]; var patternDigitsPrivite = @"(\d)"; var patternDigitsLettersPrivite = @"([\dA-Za-z])"; var patternDigitsBrodcast = @"([^\d])"; var patternDigitsLettersBrodcast = @"([\dA-Za-z])"; MatchCollection matchesPriviteFirst = Regex.Matches(firstQuery, patternDigitsPrivite); MatchCollection matchesPriviteSecond = Regex.Matches(secondQuery, patternDigitsLettersPrivite); MatchCollection matchesBrodcastFirst = Regex.Matches(firstQuery, patternDigitsBrodcast); MatchCollection matchesBrodcastSecond = Regex.Matches(secondQuery, patternDigitsLettersBrodcast); if (matchesPriviteFirst.Count == firstQuery.Length && matchesPriviteSecond.Count == secondQuery.Length)//privite { var currentPrivite = new Privite(); currentPrivite.RecipientCode = Reverse(firstQuery); currentPrivite.Massage = secondQuery; privites.Add(currentPrivite); } else if (matchesBrodcastFirst.Count == firstQuery.Length && matchesBrodcastSecond.Count == secondQuery.Length)//brodcast { var currentBrodcast = new Broadcast(); currentBrodcast.Massage = firstQuery; var secondQueryList = new List <string>(); for (int i = 0; i < secondQuery.Length; i++) { if (secondQuery[i] >= 97 && secondQuery[i] <= 122) { secondQueryList.Add(secondQuery[i].ToString().ToUpper()); } else if (secondQuery[i] >= 65 && secondQuery[i] <= 90) { secondQueryList.Add(secondQuery[i].ToString().ToLower()); } else { secondQueryList.Add(secondQuery[i].ToString()); } } currentBrodcast.Frequency = string.Join("", secondQueryList); broadcastes.Add(currentBrodcast); } } line = Console.ReadLine(); } Console.WriteLine("Broadcasts:"); if (broadcastes.Count == 0) { Console.WriteLine("None"); } else { foreach (var broadcast in broadcastes) { Console.WriteLine($"{broadcast.Frequency} -> {broadcast.Massage}"); } } Console.WriteLine("Messages:"); if (broadcastes.Count == 0) { Console.WriteLine("None"); } else { foreach (var privit in privites) { Console.WriteLine($"{privit.RecipientCode} -> {privit.Massage}"); } } }
static void Main() { checked { var input = Console.ReadLine(); char[] splitters = { ' ', '-', '>', '<' }; var messageList = new List <Message>(); var broadcastList = new List <Broadcast>(); while (input != "Hornet is Green") { Message message = new Message(); Broadcast broadcast = new Broadcast(); var splittedInput = Regex.Split(input, " <-> ").ToArray(); if (splittedInput.Length == 1) { input = Console.ReadLine(); break; } var splittedInput1 = splittedInput[0].ToCharArray(); var splittedInput2 = splittedInput[1].ToCharArray(); bool ContainsOnlyDigit = true; bool ContainsDigitsAndLetter = true; bool DoesNotContainDigit = true; for (int i = 0; i < splittedInput1.Length; i++) { if (!char.IsDigit(splittedInput1[i])) { ContainsOnlyDigit = false; } else { DoesNotContainDigit = false; } } for (int i = 0; i < splittedInput2.Length; i++) { if (!char.IsLetterOrDigit(splittedInput2[i])) { ContainsDigitsAndLetter = false; break; } } if (ContainsDigitsAndLetter && ContainsOnlyDigit) { StringBuilder sb = new StringBuilder(); for (int i = splittedInput1.Length - 1; i >= 0; i--) { sb.Append(splittedInput1[i]); } message.Code = sb.ToString(); message.Mes = splittedInput[1]; messageList.Add(message); } else if (DoesNotContainDigit && ContainsDigitsAndLetter) { StringBuilder sb = new StringBuilder(); var freq = splittedInput[1].ToCharArray(); for (int i = 0; i < freq.Length; i++) { if (char.IsUpper(freq[i])) { sb.Append(char.ToLower(freq[i])); } else if (char.IsLower(freq[i])) { sb.Append(char.ToUpper(freq[i])); } else { sb.Append(freq[i]); } } broadcast.Mes = splittedInput[0]; broadcast.Frequency = sb.ToString(); broadcastList.Add(broadcast); } input = Console.ReadLine(); } PrintResult(messageList, broadcastList); } }