Home »
.Net »
C# Programs
C# program to change the case of entered character
Here, we are going to learn how to change the case of entered character in C#?
By Nidhi Last updated : April 15, 2023
Changing Case of a Character
Here we will change the case of entered character; if we enter a character in lowercase then the character will be converted into uppercase or if we enter a character in uppercase then the character will be converted into lowercase.
C# code for changing case of a character
The source code to change the case of entered character is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to change the case of entered character.
using System;
class CaseDemo
{
static void Main(string[] args)
{
char ch;
Console.Write("Enter a character : ");
ch = Convert.ToChar(Console.ReadLine());
if (ch >= 65 && ch <= 90)
{
Console.WriteLine("Convert Character '"+ch+"' into : '"+char.ToLower(ch)+"'");
}
else if (ch >= 97 && ch <= 122)
{
Console.WriteLine("Convert Character '" + ch + "' into : '" + char.ToUpper(ch) + "'");
}
}
}
Output
Enter a character : k
Convert Character 'k' into : 'K'
Press any key to continue . . .
Explanation
In the above program, we created a class CaseDemo that contains the Main() method. In the Main() method, we read a character from the keyboard.
if (ch >= 65 && ch <= 90)
{
Console.WriteLine("Convert Character '"+ch+"' into : '"+char.ToLower(ch)+"'");
}
In the above code, we checked entered character is an uppercase character or not. Because the ASCII value of 'A' is 65 and the ASCII value of 'Z' is 90. Then here we converted the entered character into lowercase character.
else if (ch >= 97 && ch <= 122)
{
Console.WriteLine("Convert Character '" + ch + "' into : '" + char.ToUpper(ch) + "'");
}
In the above code, we checked entered character is a lowercase character or not. Because the ASCII value of 'a' is 97 and the ASCII value of 'z' is 122. Then here we converted the entered character into uppercase character.
C# Basic Programs »