encho253

Untitled

Nov 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. /// <summary>
  5. /// Write a program that replaces in a HTML document given as string all the tags
  6. /// <a href="URL">TEXT</a> with corresponding markdown notation [TEXT](URL).
  7. /// https://github.com/TelerikAcademy/CSharp-Part-2/tree/master/Topics/06.%20Strings-and-Text-Processing/homework/15.%20Replace%20tags
  8. /// </summary>
  9. class ReplaceTags
  10. {
  11.     /// <summary>
  12.     /// Defines the entry point of the application.
  13.     /// </summary>
  14.     static void Main()
  15.     {
  16.         string text = Console.ReadLine();
  17.         string finalText = ReplaceTagsInHTML(text);
  18.         Console.WriteLine(finalText);
  19.     }
  20.     /// <summary>
  21.     /// Replaces the tags in HTML.
  22.     /// </summary>
  23.     /// <param name="text">The text.</param>
  24.     /// <returns></returns>
  25.     static string ReplaceTagsInHTML(string text)
  26.     {
  27.         int startPosition = text.IndexOf("<a href=");
  28.         int nameSitePosition = 0;
  29.         int endNameSitePosition = 0;
  30.         string siteName = null;
  31.         string linkName = null;
  32.         int newBuilder = 0;
  33.      
  34.         StringBuilder builder = new StringBuilder();
  35.         StringBuilder finalBuilder = new StringBuilder();
  36.  
  37.         while (true)
  38.         {
  39.             if (text.IndexOf("<a href=", startPosition) != -1)
  40.             {
  41.                 startPosition = text.IndexOf("<a href=",startPosition) + 9;
  42.                 nameSitePosition = text.IndexOf("\">", startPosition);
  43.                 endNameSitePosition = text.IndexOf("</a>",startPosition);
  44.              
  45.                 for (int i = nameSitePosition + 2; i < endNameSitePosition ; i++)
  46.                 {
  47.                    builder.Append(text[i]);
  48.                 }
  49.                 siteName = builder.ToString();
  50.                 builder.Clear();
  51.  
  52.                 for (int i = startPosition; i < nameSitePosition; i++)
  53.                 {
  54.                     builder.Append(text[i]);
  55.                 }
  56.                 linkName = builder.ToString();
  57.                 builder.Clear();
  58.  
  59.                 for (int i = newBuilder; i < text.Length; i++)
  60.                 {
  61.                     if (i == startPosition - 9)
  62.                     {
  63.                         builder.Append("[").Append(siteName).Append("]").Append("(").Append(linkName).Append(")");
  64.                         newBuilder = endNameSitePosition + 4;
  65.                         startPosition = newBuilder;
  66.                         i = newBuilder;
  67.  
  68.                         if (text.IndexOf("<a href=", startPosition) != -1)
  69.                         {                          
  70.                             break;
  71.                         }
  72.                         else
  73.                         {
  74.                             i--;
  75.                             continue;
  76.                         }                                          
  77.                     }                
  78.                     else
  79.                     {
  80.                         builder.Append(text[i]);
  81.                     }
  82.                 }
  83.                 finalBuilder.Append(builder.ToString());
  84.                 builder.Clear();
  85.             }
  86.             else
  87.             {
  88.                 break;
  89.             }                      
  90.         }
  91.         return finalBuilder.ToString();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment