Created
February 2, 2018 10:30
-
-
Save Danielkaas94/a856f3e2a98f48849f53f529449e7385 to your computer and use it in GitHub Desktop.
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
/// <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; | |
} | |
} |
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
/// <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