Skip to content

Instantly share code, notes, and snippets.

@tafo
Last active August 2, 2020 10:16
Show Gist options
  • Save tafo/69730cec0988d480de4070cfa60f34b2 to your computer and use it in GitHub Desktop.
Save tafo/69730cec0988d480de4070cfa60f34b2 to your computer and use it in GitHub Desktop.
public bool DetectCapitalUse(string word)
{
var n = word.Length;
bool match1 = true, match2 = true, match3 = true;
// case 1: All capital
for (int i = 0; i < n; i++)
{
if (!char.IsUpper(word[i]))
{
match1 = false;
break;
}
}
if (match1)
{
return true;
}
// case 2: All not capital
for (int i = 0; i < n; i++)
{
if (!char.IsLower(word[i]))
{
match2 = false;
break;
}
}
if (match2)
{
return true;
}
// case 3: All not capital except first
if (!char.IsUpper(word[0]))
{
match3 = false;
}
if (match3)
{
for (int i = 1; i < n; i++)
{
if (!char.IsLower(word[i]))
{
match3 = false;
break;
}
}
}
if (match3)
{
return true;
}
// if not matching
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment