Created
April 19, 2021 09:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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