Advertisement
IvayloGeorgiev

Fake Text Markup Language

Jan 20th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class FakeMarkup
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         int n = int.Parse(Console.ReadLine());
  11.         string pattern = "<(.*?)>";
  12.         string lines = string.Empty;
  13.  
  14.         for (int count = 0; count < n; count++) lines += "\n" + Console.ReadLine();
  15.  
  16.         Match prev = Regex.Match(lines, pattern);
  17.         while (prev.Success)
  18.         {
  19.             Match next = prev.NextMatch();
  20.             if (next.Groups[1].Value == ("/" + prev.Groups[1].Value))
  21.             {
  22.                 string original = lines.Substring(prev.Index, next.Index + next.Length - prev.Index);
  23.                    
  24.                 int start = prev.Index + prev.Length;
  25.                 int end = next.Index - start;
  26.                 string affected = lines.Substring(start, end);
  27.                    
  28.                 lines = lines.Replace(original, Change(prev.Value, affected));
  29.                 prev = Regex.Match(lines, pattern);
  30.             }
  31.             else prev = next;
  32.         }
  33.         Console.WriteLine(lines);
  34.     }
  35.  
  36.     static string Change(string action, string text)
  37.     {
  38.         switch (action)
  39.         {
  40.             case "<upper>":
  41.                 {
  42.                     return text.ToUpper();
  43.                 }
  44.             case "<lower>":
  45.                 {
  46.                     return text.ToLower();
  47.                        
  48.                 }
  49.             case "<toggle>":
  50.                 {
  51.                     char[] result = text.ToCharArray();
  52.                     for (int i = 0; i < result.Length; i++)
  53.                     {
  54.                             if (char.IsLower(result[i])) result[i] = char.ToUpper(result[i]);
  55.                             else if (char.IsUpper(result[i])) result[i] = char.ToLower(result[i]);
  56.                     }
  57.                     return (new string(result));
  58.                 }
  59.             case "<rev>":
  60.                 {
  61.                     return new string(text.Reverse().ToArray());
  62.                 }
  63.         }
  64.         return string.Empty;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement