C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Main: The Program class introduces the Main entry point, where we test the IsUpper and IsLower extension methods on various string literals.
String LiteralInfo: A string is defined as uppercase if it contains no lowercase letters. It is defined as lowercase if it contains no uppercase letters.
Note: This is not the same as a string containing only uppercase or only lowercase letters.
Tip: You could change the implementation of these methods to fit this requirement if necessary.
C# program that implements string IsUpper and IsLower
using System;
static class Extensions
{
public static bool IsUpper(this string value)
{
// Consider string to be uppercase if it has no lowercase letters.
for (int i = 0; i < value.Length; i++)
{
if (char.IsLower(value[i]))
{
return false;
}
}
return true;
}
public static bool IsLower(this string value)
{
// Consider string to be lowercase if it has no uppercase letters.
for (int i = 0; i < value.Length; i++)
{
if (char.IsUpper(value[i]))
{
return false;
}
}
return true;
}
}
class Program
{
static void Main()
{
Console.WriteLine("test".IsLower()); // True
Console.WriteLine("test".IsUpper());
Console.WriteLine("Test".IsLower());
Console.WriteLine("Test".IsUpper());
Console.WriteLine("TEST3".IsLower());
Console.WriteLine("TEST3".IsUpper()); // True
}
}
Output
True
False
False
False
False
True
Review: The IsUpper and IsLower methods can provide a clearer method call syntax as well as better performance for this test.