using System.Collections.Generic;
public class AutoCompleteHTML
public static (string tag, int position) autoComplete(string s)
List<string> linesOfCode = new List<string>();
System.Text.StringBuilder line = new System.Text.StringBuilder();
linesOfCode.Add(line.ToString());
linesOfCode.Add(line.ToString());
List<string> selfClosedTags = new List<string> { "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr" };
Stack<string> stack = new Stack<string>();
foreach (string lineCode in linesOfCode)
for (int j = 0; j < lineCode.Length;)
if (j + 1 < lineCode.Length && lineCode[j] == '<' && lineCode[j + 1] == '/')
System.Text.StringBuilder tag = new System.Text.StringBuilder();
while (j < lineCode.Length && char.IsLower(lineCode[j]))
while (j < lineCode.Length && lineCode[j] != '>')
if (stack.Count > 0 && !stack.Peek().Equals(tag.ToString()))
return ("</" + stack.Peek() + ">", count * j - 3);
else if (j + 1 < lineCode.Length && lineCode[j] == '<' && lineCode[j + 1] == '!')
else if (lineCode[j] == '<')
System.Text.StringBuilder tag = new System.Text.StringBuilder();
while (j < lineCode.Length && char.IsLower(lineCode[j]))
while (j < lineCode.Length && lineCode[j] != '>')
if (!selfClosedTags.Contains(tag.ToString()))
stack.Push(tag.ToString());
return ("</" + stack.Peek() + ">", s.Length);
public static void Main(string[] args)
string s = "<p><b>(TARIFFE DEDICATE – si veda htl directory) code DSV</b></p><p></p><p><b>•CAREER BAND 1-2: fino a 1.000$ a notte, tasse escluse</b></p><p><b>•CAREER BAND 3-4: fino a 700$ a notte, tasse escluse</b></p><p><b>•CAREER BAND 5-11: fino a 400$ a notte, tasse escluse</b></p><p><b>•CAREER BAND 00: nessuna restrizione, il booker avrà già effettuato il processo autorizzativo interno. </b></p><p><b></p><p><b></p><p></p><p></p>";
(string tag, int position) text = (string.Empty, 0);
s = s.Insert(text.position, text.tag);
Console.WriteLine(text.tag + " - " + text.position);