Skip to content

Instantly share code, notes, and snippets.

@mormegil-cz
Last active December 27, 2020 10:04
Show Gist options
  • Save mormegil-cz/6634304 to your computer and use it in GitHub Desktop.
Save mormegil-cz/6634304 to your computer and use it in GitHub Desktop.
void CountAccents(string input)
{
if (input.Length != 0 && !Char.IsWhiteSpace(input[input.Length - 1])) input = input + " ";
int wordsTotal = 0;
int wordsOnlyLetters = 0;
int wordsAccented = 0;
int wordStart = 0;
bool hasAccent = false;
bool hasNonLetter = false;
for (int i = 0; i < input.Length; ++i)
{
var c = input[i];
if (Char.IsWhiteSpace(c) || Char.IsSeparator(c) || Char.IsPunctuation(c))
{
int wordLength = i - wordStart;
if (wordLength > 0)
{
++wordsTotal;
if (!hasNonLetter && wordLength > 0) ++wordsOnlyLetters;
if (!hasNonLetter && wordLength > 0 && hasAccent) ++wordsAccented;
}
wordStart = i + 1;
hasNonLetter = hasAccent = false;
}
else if (!Char.IsLetter(c))
{
hasNonLetter = true;
}
else if (c > '\u0080')
{
hasAccent = true;
}
}
Console.WriteLine("{0} words total, {1} real words, {2} ({3:N1} %) with accents", wordsTotal, wordsOnlyLetters, wordsAccented, 100.0 * wordsAccented / wordsOnlyLetters);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment