static void Main(string[] args) { string pattern1 = @"^([0-9]+) \<\-\> ([A-Za-z0-9]+)$"; string pattern2 = @"^([^0-9]+) \<\-\> ([A-Za-z0-9]+)$"; var privateMsgs = new List <PrivateMessage>(); var broadcasts = new List <Broadcast>(); string input = Console.ReadLine(); while (input != "Hornet is Green") { if (Regex.IsMatch(input, pattern1)) { Match m = Regex.Match(input, pattern1); string code = ReverseString(m.Groups[1].Value); string msg = m.Groups[2].Value; PrivateMessage pm = new PrivateMessage(); pm.Message = msg; pm.Code = code; privateMsgs.Add(pm); } else if (Regex.IsMatch(input, pattern2)) { Match m = Regex.Match(input, pattern2); string frequency = ReverseCharacters(m.Groups[2].Value); string msg = m.Groups[1].Value; Broadcast b = new Broadcast(); b.Message = msg; b.Frequency = frequency; broadcasts.Add(b); } input = Console.ReadLine(); } Console.WriteLine("Broadcasts:"); if (broadcasts.Count > 0) { foreach (var item in broadcasts) { Console.WriteLine($"{item.Frequency} -> {item.Message}"); } } else { Console.WriteLine("None"); } Console.WriteLine("Messages:"); if (privateMsgs.Count > 0) { foreach (var item in privateMsgs) { Console.WriteLine($"{item.Code} -> {item.Message}"); } } else { Console.WriteLine("None"); } }
public static void Main() { List <Broadcast> broadcasts = new List <Broadcast>(); List <Message> messages = new List <Message>(); string privateMessagePattern = @"(^\d+)\s\<\-\>\s([A-Za-z0-9]+)$"; string broadcastPattern = @"(^\D+)\s\<\-\>\s([A-Za-z0-9]+)$"; Regex privateRegex = new Regex(privateMessagePattern); Regex broadcastRegex = new Regex(broadcastPattern); string input = Console.ReadLine(); while (input != "Hornet is Green") { Match privateMatch = privateRegex.Match(input); Match broadcastMatch = broadcastRegex.Match(input); if (privateMatch.Success) { string recipientCode = privateMatch.Groups[1].Value; string message = privateMatch.Groups[2].Value; var recipientCodeReverse = new string(recipientCode.Reverse().ToArray()); Message newMessage = new Message() { Recipient = recipientCodeReverse, Mes = message }; messages.Add(newMessage); } else if (broadcastMatch.Success) { string message = broadcastMatch.Groups[1].Value; string frequency = broadcastMatch.Groups[2].Value; string newFrequency = string.Empty; foreach (var symbol in frequency) { if (symbol >= 65 && symbol <= 90) { newFrequency += symbol.ToString().ToLower(); } else if (symbol >= 97 && symbol <= 122) { newFrequency += symbol.ToString().ToUpper(); } else { newFrequency += symbol; } } Broadcast newBroadcast = new Broadcast() { Frequency = newFrequency, Message = message }; broadcasts.Add(newBroadcast); } input = Console.ReadLine(); } Console.WriteLine("Broadcasts:"); if (broadcasts.Count > 0) { foreach (Broadcast broadcast in broadcasts) { Console.WriteLine($"{broadcast.Frequency} -> {broadcast.Message}"); } } else { Console.WriteLine("None"); } Console.WriteLine("Messages:"); if (messages.Count > 0) { foreach (Message message in messages) { Console.WriteLine($"{message.Recipient} -> {message.Mes}"); } } else { Console.WriteLine("None"); } }