Advertisement
zh_stoqnov

Text_Transformer

Jun 2nd, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 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 Text_Transformer
  9. {
  10. class Program
  11. {
  12. public static int getWeight(string ch)
  13. {
  14. int weight = 0;
  15.  
  16. switch (ch)
  17. {
  18. case "$":
  19. weight = 1;
  20. break;
  21. case "%":
  22. weight = 2;
  23. break;
  24. case "&":
  25. weight = 3;
  26. break;
  27. case "\'":
  28. weight = 4;
  29. break;
  30. }
  31.  
  32. return weight;
  33. }
  34.  
  35. static void Main(string[] args)
  36. {
  37. StringBuilder sb = new StringBuilder();
  38. while (true)
  39. {
  40. string input = Console.ReadLine();
  41. if (input.Equals("burp"))
  42. {
  43. break;
  44. }
  45. sb.Append(input);
  46. }
  47.  
  48. string gibberishStr = sb.ToString();
  49. gibberishStr = Regex.Replace(gibberishStr, @"\s+", " ");
  50.  
  51. string pattern = @"([$%&'])([^$%&']+)\1";
  52. Regex textBetweenSpecialChars = new Regex(pattern);
  53. MatchCollection matchCollection = Regex.Matches(gibberishStr, pattern);
  54.  
  55. List<string> words = new List<string>();
  56. List<string> specialSymbols = new List<string>();
  57. for (int i = 0; i < matchCollection.Count; i++)
  58. {
  59. var match = matchCollection[i].Groups[2].ToString();
  60. words.Add(match);
  61. specialSymbols.Add(matchCollection[i].Groups[1].ToString());
  62. }
  63.  
  64. List<string> resultWords = new List<string>();
  65. for (int i = 0; i < words.Count; i++)
  66. {
  67. var chars = words[i].ToCharArray();
  68. var specialSymbol = specialSymbols[i];
  69. StringBuilder word = new StringBuilder();
  70. for (int j = 0; j < chars.Length; j++)
  71. {
  72. char ch = chars[j];
  73. char transformedChar = Char.MinValue;
  74. if (j%2 == 0)
  75. {
  76. transformedChar = (char) (ch + getWeight(specialSymbol));
  77. }
  78. else
  79. {
  80. transformedChar = (char) (ch - getWeight(specialSymbol));
  81. }
  82. word.Append(transformedChar);
  83. }
  84. resultWords.Add(word.ToString());
  85. }
  86. Console.WriteLine(String.Join(" ", resultWords));
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement