Skip to content

Instantly share code, notes, and snippets.

@bolorundurowb
Created April 19, 2021 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bolorundurowb/14c673d74d84eadbd81f7b544a414e9f to your computer and use it in GitHub Desktop.
Save bolorundurowb/14c673d74d84eadbd81f7b544a414e9f to your computer and use it in GitHub Desktop.
A simple C# script to count and display character frequency in a given string
public class Program
{
public static void Main()
{
var sentence = AnsiConsole.Prompt(new TextPrompt<string>("What's your [green]sentence[/]?")
.Validate(input =>
string.IsNullOrWhiteSpace(input)
? ValidationResult.Error("An input is required!")
: ValidationResult.Success()));
var chars = Enumerable.Range(65, 26)
.Select(x => (char) x)
.ToList();
var table = new Table();
chars.ForEach(x => table.AddColumn(new TableColumn(x.ToString()).Centered()));
table.AddRow(chars.Select(x =>
$"[green]{sentence.Count(y => y.ToString().Equals(x.ToString(), StringComparison.InvariantCultureIgnoreCase))}[/]")
.ToArray());
AnsiConsole.Render(table);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment