Skip to content

Instantly share code, notes, and snippets.

@sammysheep
Last active May 24, 2023 12:56
Show Gist options
  • Save sammysheep/a02419086b600601c7d2c87253dd5f7c to your computer and use it in GitHub Desktop.
Save sammysheep/a02419086b600601c7d2c87253dd5f7c to your computer and use it in GitHub Desktop.
SmithClass-WordCount
// Word Count - counts letters, words, and lines similar to Unix `wc`
string filename = "";
if (args.Length != 1)
{
Console.WriteLine($"\nUsage:\n\tdotnet run <filename>\n");
System.Environment.Exit(1);
}
else
{
filename = args[0];
if (!File.Exists(filename))
{
Console.WriteLine($"No file found named '{filename}'");
System.Environment.Exit(1);
}
}
var data = File.ReadAllText(filename);
// Initialize variables
int lines = // NEED A VALUE;
int words = // NEED A VALUE;
/* NEED A TYPE */ letters = 0;
/* NEED A TYPE */ was_word_letter = false;
// loop through data one character at a time
foreach (var letter in data)
{
// INCREMENT ONE OF: WORDS, LINES, or LETTERS
// letter is a newline
if (letter == '\n')
{
// INCREMENT ONE OF: WORDS, LINES, or LETTERS
}
// whitespace detected
if (letter == ' ' || letter == '\n' || letter == '\t' )
{
was_word_letter = false;
}
// otherwise, word letter detected
else
{
// previously wasn't a word letter
if (!was_word_letter)
{
// INCREMENT ONE OF: WORDS, LINES, or LETTERS
}
was_word_letter = true;
}
}
Console.WriteLine($"\t{lines}\t{words}\t{letters} {filename}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment