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;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace _1_Exam_13May2016_3_BasicMarkLanguage
- {
- class Program
- {
- static void Main()
- {
- string input = Console.ReadLine();
- int n = 1;
- while (input!="<stop/>")
- {
- string pattern = @"^\s*<\s*([a-z]+)\s+([a-z]+\s*=\s*""\s*([0-9]+)\s*""\s*)?[a-z]+\s*=\s*""([^""]*)""\s*\/\s*>\s*$";
- Match match = Regex.Match(input, pattern);
- string command = match.Groups[1].ToString();
- string value = match.Groups[3].ToString();
- string content = match.Groups[4].ToString();
- string result = "";
- if (content.Length > 0)
- {
- switch (command)
- {
- case "inverse": result = Inverse(content); Console.WriteLine("{0}. {1}", n, result); n++; break;
- case "reverse": result = Reverse(content); Console.WriteLine("{0}. {1}", n, result); n++; break;
- case "repeat":
- if (int.Parse(value) > 0)
- {
- for (int i = 1; i <= int.Parse(value); i++)
- {
- Console.WriteLine("{0}. {1}", n, content);
- n++;
- }
- }
- break;
- default: break;
- }
- }
- input = Console.ReadLine();
- }
- }
- private static string Reverse(string content)
- {
- StringBuilder sb = new StringBuilder();
- for (int i=content.Length-1; i >= 0; i--)
- {
- sb.Append(content[i]);
- }
- content = sb.ToString();
- return content;
- }
- private static string Inverse(string content)
- {
- StringBuilder sb = new StringBuilder();
- foreach (var item in content)
- {
- if ( char.IsLower(item)) { sb.Append(char.ToUpper(item)); }
- else if (char.IsUpper(item)) { sb.Append(char.ToLower(item)); }
- else { sb.Append(item); }
- }
- content = sb.ToString();
- return content;
- }
- }
- }
- // My older solution 100 from 100:
- //string input = Console.ReadLine();
- //string patternContent = @"content\s*=\s*""([^""]*)""";
- //string patternCommand = @"inverse|reverse|repeat\s+value\s*=\s*""\s*([\d]+)\s*""";
- //int counter = 0;
- // while ((!input.Contains("stop")) && input.Contains("content"))
- // {
- // Match matchCont = Regex.Match(input, patternContent);
- //string content = matchCont.Groups[1].ToString();
- // if (content == "") { input = Console.ReadLine(); continue; }
- // Match matchCom = Regex.Match(input, patternCommand);
- //string resultContent = "";
- // if (matchCom.ToString() == "inverse")
- // {
- // for (int i = 0; i<content.Length; i++)
- // {
- // string letter = "";
- // if (content[i] >= 65 && content[i] <= 90)
- // { letter = content[i].ToString().ToLower(); resultContent += letter; }
- // else { letter = content[i].ToString().ToUpper(); resultContent += letter; }
- // }
- // counter++;
- // Console.WriteLine("{0}. {1}", counter, resultContent);
- // }
- // else if (matchCom.ToString() == "reverse")
- // {
- // char[] reversed = content.Reverse().ToArray();
- //resultContent = string.Join("", reversed);
- //counter++;
- // Console.WriteLine("{0}. {1}", counter, resultContent);
- // }
- // else
- // {
- // string repeatTimesStr = matchCom.Groups[1].ToString();
- //int repeatTimes = int.Parse(repeatTimesStr);
- // for (int i = 0; i<repeatTimes; i++)
- // {
- // counter++;
- // Console.WriteLine("{0}. {1}", counter, content);
- // }
- // if (repeatTimes == 0)
- // {
- // input = Console.ReadLine(); continue;
- // }
- // }
- // input = Console.ReadLine();
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement