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#?
Submitted by Nidhi, on September 14, 2020

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.

Program:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.