The issue arises because HTML treats angle brackets (< and >) as part of its markup syntax, so they need to be escaped to display properly within a <code> or <pre> tag.
Solution
Replace < with < and > with > Here's your code with the corrections:
Angle brackets Program <>
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
Dictionary<char, int> charCount = CountCharacters(input);
Console.WriteLine("Character occurrences:");
foreach (var pair in charCount)
{
Console.WriteLine($"'{pair.Key}': {pair.Value}");
}
}
static Dictionary<char, int> CountCharacters(string str)
{
var result = new Dictionary<char, int>();
foreach (char c in str)
{
if (result.ContainsKey(c))
result[c]++;
else
result[c] = 1;
}
return result;
}
}
Explanation
- <: Escapes the < symbol.
- >: Escapes the > symbol.
Rendered Output
The angle brackets will now display correctly on your HTML page.