w3resource

C#: Check if a given string contains only lowercase or uppercase characters


Check All Uppercase or Lowercase

Write a C# Sharp program to check if a given string contains only lowercase or uppercase characters.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            // Calls the test function with different string arguments and prints the results
            Console.WriteLine(test("PHP")); // Output: True
            Console.WriteLine(test("python")); // Output: True
            Console.WriteLine(test("JavaScript")); // Output: False
        }

        // Function to check if a string contains both uppercase and lowercase characters
        public static bool test(string str1)
        {
            // Checking if the input string is equal to its uppercase version OR its lowercase version
            // If the string is either completely uppercase or completely lowercase, it returns true, otherwise false
            return str1 == str1.ToUpper() || str1 == str1.ToLower();
        }
    }
}

Sample Output:

True
True
False

Flowchart:

Flowchart: C# Sharp Exercises - Check if a given string contains only lowercase or uppercase characters.

Sample Solution-1:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            // Calls the test function with different string arguments and prints the results
            Console.WriteLine(test("PHP")); // Output: True
            Console.WriteLine(test("python")); // Output: True
            Console.WriteLine(test("JavaScript")); // Output: False
        }

        // Function to check if a string contains characters in either all uppercase or all lowercase
        public static bool test(string str1)
        {
            // Checking if the input string is entirely in uppercase OR entirely in lowercase
            // If the string is either completely uppercase or completely lowercase, it returns true, otherwise false
            return str1 == str1.ToUpper() || str1 == str1.ToLower();
        }
    }
}

Sample Output:

True
True
False

Flowchart:

Flowchart: C# Sharp Exercises - Check if a given string contains only lowercase or uppercase characters.

For more Practice: Solve these Related Problems:

  • Write a C# program to check if a string contains only alphabetic characters.
  • Write a C# program to check whether all characters are either uppercase or numeric digits.
  • Write a C# program to check if a string starts with an uppercase letter and ends with a lowercase letter.
  • Write a C# program to return true only if a string is in title case (first letter uppercase, rest lowercase).

Go to:


PREV : Count Specific Character in String.
NEXT : Remove First and Last Characters.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.