C# program to change the case of entered character

C# program to change the case of entered character:

In this post, we will learn how to change the case of a entered character in C#. If the user will enter a lower case character, it will print the same character in uppercase. For a uppercase character, it will print the same character in lower case.

With this program, you will learn how to read user inputs and how to change the case of a character in C#.

C# program:

Below is the complete C# program:

using System;

public class Program
{
	public static void Main()
	{
		char c;
        
        Console.Write("Enter a character to convert : ");
        c = Convert.ToChar(Console.ReadLine());
        
        if (c >= 65 && c <= 90)
        {
            Console.WriteLine("Changing the character to lowercase "+char.ToLower(c));
        }
        else if (c >= 97 && c <= 122)
        {
            Console.WriteLine("Changing the character to uppercase "+char.ToUpper(c));
        }
	}
}

Explanation:

Here,

  • we are asking the user to enter a character to convert or change the case.
  • The entered character is stored in the variable c.
  • We are checking the ASCII value of the entered character. Based on that, if it is a lower case, we are converting it to uppercase. Else, we are converting it to lower case.
  • char.ToLower is used to convert a character to lower case and char.ToUpper to convert a character to upper case.

Sample output:

It will print output as like below:

Enter a character to convert : F
Changing the character to lowercase f

Enter a character to convert : f
Changing the character to uppercase F

You might also like: