Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JL-Cox/a7c9377a95f8ce0adcad247d5b1a2cc9 to your computer and use it in GitHub Desktop.
Save JL-Cox/a7c9377a95f8ce0adcad247d5b1a2cc9 to your computer and use it in GitHub Desktop.
CodeWarsKata: Syntax Highlighting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
namespace CodeWarsKata_RoboScript1_SyntaxHighlighting
{
class Program
{
static void Main(string[] args)
{
string code = "FFFR(345)F2LL";
Console.WriteLine(Highlight(code));
}
public static string Highlight(string code)
{
//F - <span style="color: pink">F</span>
//L - <span style="color: red">L</span>
//R - <span style="color: green">R</span>
//0 => 9 - <span style="color: orange">#</span>
//() - Do not highlight, just add to answer string
foreach (var item in code)
{
Console.Write(item);
}
Console.WriteLine();
string answer = "";
for (int i = 0; i < code.Length; i++)
{
//F
if (code.ElementAt(i) == 'F')
{
answer += "<span style=\"color: pink\">F";
if (i != (code.Length - 1))
{
if (code.ElementAt(i + 1) == 'F' && i != (code.Length - 1))
{
while (i != (code.Length - 1) && code.ElementAt(i + 1) == 'F')
{
answer += "F";
i++;
}
answer += "</span>";
}
else answer += "</span>";
}
else answer += "</span>";
}
//L
if (code.ElementAt(i) == 'L')
{
answer += "<span style=\"color: red\">L";
if (i != (code.Length - 1))
{
if (code.ElementAt(i + 1) == 'L' && i != (code.Length - 1))
{
while (i != (code.Length - 1) && code.ElementAt(i + 1) == 'L')
{
answer += "L";
i++;
}
answer += "</span>";
}
else answer += "</span>";
}
else answer += "</span>";
}
//R
if (code.ElementAt(i) == 'R')
{
answer += "<span style=\"color: green\">R";
if (i != (code.Length - 1))
{
if (code.ElementAt(i + 1) == 'R')
{
while (i != (code.Length - 1) && code.ElementAt(i + 1) == 'R')
{
answer += "R";
i++;
}
answer += "</span>";
}
else answer += "</span>";
}
else answer += "</span>";
}
//0
if (char.IsDigit(code.ElementAt(i)) == true)
{
answer += $"<span style=\"color: orange\">{code.ElementAt(i)}";
if (i != (code.Length - 1))
{
if (char.IsDigit(code.ElementAt(i + 1)) == true && i != (code.Length - 1))
{
while (i != (code.Length - 1) && char.IsDigit(code.ElementAt(i + 1)) == true)
{
answer += code.ElementAt(i + 1);
i++;
}
answer += "</span>";
}
else answer += "</span>";
}
else answer += "</span>";
}
//()
if (code.ElementAt(i) == '(' || code.ElementAt(i) == ')')
{
answer += $"{code.ElementAt(i)}";
}
}
Console.WriteLine(answer);
Console.ReadKey();
return answer;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment