Skip to content

Instantly share code, notes, and snippets.

@Danielkaas94
Created February 2, 2018 10:30
Show Gist options
  • Save Danielkaas94/a856f3e2a98f48849f53f529449e7385 to your computer and use it in GitHub Desktop.
Save Danielkaas94/a856f3e2a98f48849f53f529449e7385 to your computer and use it in GitHub Desktop.
/// <summary>
/// Check to see if a string has the same amount of 'x's and 'o's.
/// The method must return a boolean and be case insensitive.
/// </summary>
/// <param name="input">The string can contain any char.</param>
/// <returns></returns>
public static bool XO(string input)
{
int xCounter = 0;
int oCounter = 0;
foreach (char item in input)
{
if ("x" == item.ToString().ToLower())
{
xCounter++;
}
else if ("o" == item.ToString().ToLower())
{
oCounter++;
}
}
if (xCounter == oCounter)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Same as XO, just in one line.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool XO2(string input)
{
return input.ToLower().Count(i => i == 'x') == input.ToLower().Count(i => i == 'o');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment