Advertisement
plamen27

Basic Markup Language

Dec 25th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _1_Exam_13May2016_3_BasicMarkLanguage
  9. {
  10. class Program
  11. {
  12. static void Main()
  13. {
  14. string input = Console.ReadLine();
  15.  
  16. int n = 1;
  17. while (input!="<stop/>")
  18. {
  19. string pattern = @"^\s*<\s*([a-z]+)\s+([a-z]+\s*=\s*""\s*([0-9]+)\s*""\s*)?[a-z]+\s*=\s*""([^""]*)""\s*\/\s*>\s*$";
  20. Match match = Regex.Match(input, pattern);
  21. string command = match.Groups[1].ToString();
  22. string value = match.Groups[3].ToString();
  23. string content = match.Groups[4].ToString();
  24. string result = "";
  25. if (content.Length > 0)
  26. {
  27. switch (command)
  28. {
  29. case "inverse": result = Inverse(content); Console.WriteLine("{0}. {1}", n, result); n++; break;
  30. case "reverse": result = Reverse(content); Console.WriteLine("{0}. {1}", n, result); n++; break;
  31. case "repeat":
  32. if (int.Parse(value) > 0)
  33. {
  34. for (int i = 1; i <= int.Parse(value); i++)
  35. {
  36. Console.WriteLine("{0}. {1}", n, content);
  37. n++;
  38. }
  39. }
  40. break;
  41.  
  42. default: break;
  43. }
  44. }
  45.  
  46. input = Console.ReadLine();
  47. }
  48. }
  49.  
  50. private static string Reverse(string content)
  51. {
  52. StringBuilder sb = new StringBuilder();
  53. for (int i=content.Length-1; i >= 0; i--)
  54. {
  55. sb.Append(content[i]);
  56. }
  57. content = sb.ToString();
  58. return content;
  59. }
  60.  
  61. private static string Inverse(string content)
  62. {
  63. StringBuilder sb = new StringBuilder();
  64. foreach (var item in content)
  65. {
  66. if ( char.IsLower(item)) { sb.Append(char.ToUpper(item)); }
  67. else if (char.IsUpper(item)) { sb.Append(char.ToLower(item)); }
  68. else { sb.Append(item); }
  69. }
  70. content = sb.ToString();
  71. return content;
  72. }
  73.  
  74.  
  75.  
  76. }
  77. }
  78.  
  79.  
  80. // My older solution 100 from 100:
  81.  
  82. //string input = Console.ReadLine();
  83. //string patternContent = @"content\s*=\s*""([^""]*)""";
  84. //string patternCommand = @"inverse|reverse|repeat\s+value\s*=\s*""\s*([\d]+)\s*""";
  85. //int counter = 0;
  86. // while ((!input.Contains("stop")) && input.Contains("content"))
  87. // {
  88. // Match matchCont = Regex.Match(input, patternContent);
  89. //string content = matchCont.Groups[1].ToString();
  90. // if (content == "") { input = Console.ReadLine(); continue; }
  91. // Match matchCom = Regex.Match(input, patternCommand);
  92.  
  93. //string resultContent = "";
  94. // if (matchCom.ToString() == "inverse")
  95. // {
  96. // for (int i = 0; i<content.Length; i++)
  97. // {
  98. // string letter = "";
  99. // if (content[i] >= 65 && content[i] <= 90)
  100. // { letter = content[i].ToString().ToLower(); resultContent += letter; }
  101. // else { letter = content[i].ToString().ToUpper(); resultContent += letter; }
  102. // }
  103. // counter++;
  104. // Console.WriteLine("{0}. {1}", counter, resultContent);
  105. // }
  106. // else if (matchCom.ToString() == "reverse")
  107. // {
  108. // char[] reversed = content.Reverse().ToArray();
  109. //resultContent = string.Join("", reversed);
  110. //counter++;
  111. // Console.WriteLine("{0}. {1}", counter, resultContent);
  112. // }
  113. // else
  114. // {
  115. // string repeatTimesStr = matchCom.Groups[1].ToString();
  116. //int repeatTimes = int.Parse(repeatTimesStr);
  117. // for (int i = 0; i<repeatTimes; i++)
  118. // {
  119. // counter++;
  120. // Console.WriteLine("{0}. {1}", counter, content);
  121. // }
  122. // if (repeatTimes == 0)
  123. // {
  124. // input = Console.ReadLine(); continue;
  125. // }
  126. // }
  127.  
  128. // input = Console.ReadLine();
  129. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement