Conversion Between Uppercase and Lowercase Letter in C / C++ / Java / Python / C#

character case conversion

Programs for conversion between uppercase and lowercase letter have been shown here. If the letter is in uppercase, it will be converted into its lowercase form and if the letter is in lowercase, it will be converted into its uppercase form. Case conversion is achieved in two ways. We can either manipulate ASCII value of the letter or use the inbuilt functions to achieve the task.






1. Program & output for conversion between uppercase and lowercase letter using ASCII value manipulation

This section covers the C, C++, Java, Python and C# programs for conversion between uppercase and lowercase letter using ASCII value manipulation.




1.1. C Program & output for conversion between uppercase and lowercase letter using ASCII value manipulation

Code has been copied
/*******************************
	alphabetacoder.com
C Program for Conversion Between 
Uppercase and Lowercase Letter
*******************************/

#include<stdio.h>

int main() {
    // delclare variables
    char ch;

    // take input of a character
    printf("Enter a character: ");
    scanf("%c", & ch);

    //check whether the input letter
    // is in uppercase or lower case
    // If the letter is in uppercase,
    // convert it to lowercase
    // If the character is in lowercase,
    // convert it to uppercase
    // If it is another symbol, keep
    // it as it is
    if (ch >= 'A' && ch <= 'Z') {
        printf("It is an uppercase letter.\n");
        // display lowercase by adding 32 with ASCII value of ch
        printf("Lowercase of %c: %c", ch, (ch + 32));
    } else if (ch >= 'a' && ch <= 'z') {
        printf("It is an lowercase letter.\n");
        // display uppercase by subtracting 32 from ASCII value of ch
        printf("Uppercase of %c: %c", ch, (ch - 32));
    } else {
        printf("%c is not an alphabet character.\n", ch);
    }

    return 0;
}

Output


Case 1:

Enter a character: M

It is an uppercase letter.

Lowercase of M: m


Case 2:

Enter a character: s

It is an lowercase letter.

Uppercase of s: S


Case 3:

Enter a character: @

@ is not an alphabet character.





1.2. C++ Program & output for conversion between uppercase and lowercase letter using ASCII value manipulation

Code has been copied
/*********************************
	alphabetacoder.com
C++ Program for Conversion Between 
Uppercase and Lowercase Letter
*********************************/

#include<iostream>

using namespace std;

int main() {
    // delclare variables
    char ch;

    // take input of a character
    cout << "Enter a character: ";
    cin >> ch;

    //check whether the input letter
    // is in uppercase or lower case
    // If the letter is in uppercase,
    // convert it to lowercase
    // If the character is in lowercase,
    // convert it to uppercase
    // If it is another symbol, keep
    // it as it is
    if (ch >= 'A' && ch <= 'Z') {
        cout << "It is an uppercase letter." << endl;
        // display lowercase by adding 32 with ASCII value of ch
        cout << "Lowercase of " << ch << ": " << (char)(ch + 32);
    } else if (ch >= 'a' && ch <= 'z') {
        cout << "It is an lowercase letter." << endl;
        // display uppercase by subtracting 32 from ASCII value of ch
        cout << "Uppercase of " << ch << ": " << (char)(ch - 32);
    } else {
        cout << ch << " is not an alphabet character." << endl;
    }

    return 0;
}

Output


Case 1:

Enter a character: A

It is an uppercase letter.

Lowercase of A: a


Case 2:

Enter a character: u

It is an lowercase letter.

Uppercase of u: U


Case 3:

Enter a character: !

! is not an alphabet character.





1.3. Java Program & output for conversion between uppercase and lowercase letter using ASCII value manipulation

Code has been copied
/**********************************
	alphabetacoder.com
Java Program for Conversion Between 
Uppercase and Lowercase Letter
***********************************/

import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        // declare an instance of scanner class
        Scanner sc = new Scanner(System.in);

        // delclare variables
        char ch;

        // take input of a character
        System.out.print("Enter a character: ");
        ch = sc.next().charAt(0);

        //check whether the input letter
        // is in uppercase or lower case
        // If the letter is in uppercase,
        // convert it to lowercase
        // If the character is in lowercase,
        // convert it to uppercase
        // If it is another symbol, keep
        // it as it is
        if (ch >= 'A' && ch <= 'Z') {
            System.out.print("It is an uppercase letter.\n");
            // display lowercase by adding 32 with ASCII value of ch
            System.out.print("Lowercase of " + ch + ": " + (char)(ch + 32));
        } else if (ch >= 'a' && ch <= 'z') {
            System.out.print("It is an lowercase letter.\n");
            // display uppercase by subtracting 32 from ASCII value of ch
            System.out.print("Uppercase of " + ch + ": " + (char)(ch - 32));
        } else {
            System.out.print(ch + " is not an alphabet character.\n");
        }
    }
}

Output


Case 1:

Enter a character: X

It is an uppercase letter.

Lowercase of X: x


Case 2:

Enter a character: y

It is an lowercase letter.

Uppercase of y: Y


Case 3:

Enter a character: ?

? is not an alphabet character.





1.4. Python Program & output for conversion between uppercase and lowercase letter using ASCII value manipulation

Code has been copied
#**************************************
#	    alphabetacoder.com
#Python Program for Conversion Between 
#Uppercase and Lowercase Letter
#**************************************/

# take input
ch = input("Enter a character: ")

# check whether the input letter
# is in uppercase or lower case
# If the letter is in uppercase,
# convert it to lowercase
# If the character is in lowercase,
# convert it to uppercase
# If it is another symbol, keep
# it as it is
if ch >= 'A' and ch <= 'Z':
    print("It is an uppercase letter.")
    # display lowercase by adding 32 with ASCII value of ch
    print("Lowercase of", ch, ":",chr(ord(ch) + 32))
elif ch >= 'a' and ch <= 'z':
    print("It is an lowercase letter.");
    # display uppercase by subtracting 32 from ASCII value of ch
    print("Uppercase of", ch, ":",chr(ord(ch) - 32))
else:
    print(ch, "is not an alphabet character.")

Output


Case 1:

Enter a character: P

It is an uppercase letter.

Lowercase of P: p


Case 2:

Enter a character: f

It is an lowercase letter.

Uppercase of f: F


Case 3:

Enter a character: )

) is not an alphabet character.





1.5. C# Program & output for conversion between uppercase and lowercase letter using ASCII value manipulation

Code has been copied
/**********************************
	alphabetacoder.com
C# Program for Conversion Between 
Uppercase and Lowercase Letter
***********************************/

using System;

namespace UppercaseLowercase {
    class Program {
        static void Main(string[] args) {
            // declare variables
            char ch;

            // take input
            Console.Write("Enter a character: ");
            ch = Console.ReadLine()[0];

            //check whether the input letter
            // is in uppercase or lower case
            // If the letter is in uppercase,
            // convert it to lowercase
            // If the character is in lowercase,
            // convert it to uppercase
            // If it is another symbol, keep
            // it as it is
            if (ch >= 'A' && ch <= 'Z') {
                Console.WriteLine("It is an uppercase letter.");
                // display lowercase by adding 32 with ASCII value of ch
                Console.WriteLine("Lowercase of " + ch + ": " + (char)(ch + 32));
            } else if (ch >= 'a' && ch <= 'z') {
                Console.WriteLine("It is an lowercase letter.");
                // display uppercase by subtracting 32 from ASCII value of ch
                Console.WriteLine("Uppercase of " + ch + ": " + (char)(ch - 32));
            } else {
                Console.WriteLine(ch + " is not an alphabet character.");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter a character: T

It is an uppercase letter.

Lowercase of T: t


Case 2:

Enter a character: w

It is an lowercase letter.

Uppercase of w: W


Case 3:

Enter a character: #

# is not an alphabet character.





2. Program for conversion between uppercase and lowercase letter Using inbuilt functions

This section covers the C, C++, Java, Python and C# programs for conversion between uppercase and lowercase letter using inbuilt functions.




2.1. C Program for conversion between uppercase and lowercase letter using the functions toupper() and tolower()

Code has been copied
/*********************************************
	    alphabetacoder.com
C Program for Conversion Between Uppercase and 
Lowercase Letter using toupper() and tolower()
********************************************/

#include<stdio.h>

int main() {
    // delclare variables
    char ch;

    // take input of a character
    printf("Enter a character: ");
    scanf("%c", & ch);

    //check whether the input letter
    // is in uppercase or lower case
    // If the letter is in uppercase, convert it 
    // to lowercase using the function tolower()
    // If the character is in lowercase, convert it 
    // to uppercase using the function toupper()
    // If it is another character, keep
    // it as it is
    if (ch >= 'A' && ch <= 'Z') {
        printf("It is an uppercase letter.\n");
        // use tolower() function to convert
        //  the letter to lowercase
        printf("Lowercase of %c: %c", ch, tolower(ch));
    } else if (ch >= 'a' && ch <= 'z') {
        printf("It is an lowercase letter.\n");
        // use toupper() function to convert
        //  the letter to uppercase
        printf("Uppercase of %c: %c", ch, toupper(ch));
    } else {
        printf("%c is not an alphabet character.\n", ch);
    }

    return 0;
}



2.2. C++ Program for conversion between uppercase and lowercase letter using the functions toupper() and tolower()

Code has been copied
/***********************************************
	    alphabetacoder.com
C++ Program for Conversion Between Uppercase and 
Lowercase Letter using toupper() and tolower()
************************************************/

#include<iostream>

using namespace std;

int main() {
    // delclare variables
    char ch;

    // take input of a character
    cout << "Enter a character: ";
    cin >> ch;

    //check whether the input letter
    // is in uppercase or lower case
    // If the letter is in uppercase, convert it 
    // to lowercase using the function tolower()
    // If the character is in lowercase, convert it 
    // to uppercase using the function toupper()
    // If it is another character, keep
    // it as it is
    if (ch >= 'A' && ch <= 'Z') {
        cout << "It is an uppercase letter.\n";
        // use tolower() function to convert
        //  the letter to lowercase
        cout << "Lowercase of " << ch << ": " << (char) tolower(ch);
    } else if (ch >= 'a' && ch <= 'z') {
        printf("It is an lowercase letter.\n");
        // use toupper() function to convert
        //  the letter to uppercase
        cout << "Uppercase of " << ch << ": " << (char) toupper(ch);
    } else {
        cout << ch << " is not an alphabet character.\n";
    }

    return 0;
}



2.3. Java Program for conversion between uppercase and lowercase letter using the methods toUpperCase() and toLowerCase()

Code has been copied
/*****************************************************
		alphabetacoder.com
Java Program for Conversion Between Uppercase and 
Lowercase Letter using toUpperCase() and toLowerCase()
******************************************************/

import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        // declare an instance of scanner class
        Scanner sc = new Scanner(System.in);

        // delclare variables
        char ch;

        // take input of a character
        System.out.print("Enter a character: ");
        ch = sc.next().charAt(0);

        //check whether the input letter
        // is in uppercase or lower case
        // If the letter is in uppercase,
        // convert it to lowercase
        // If the character is in lowercase,
        // convert it to uppercase
        // If it is another symbol, keep
        // it as it is
        if (ch >= 'A' && ch <= 'Z') {
            System.out.print("It is an uppercase letter.\n");
            // display lowercase by using toLowerCase()
            System.out.print("Lowercase of " + ch + ": " + Character.toLowerCase(ch));
        } else if (ch >= 'a' && ch <= 'z') {
            System.out.print("It is an lowercase letter.\n");
            // display uppercase by using toUpperCase()
            System.out.print("Uppercase of " + ch + ": " + Character.toUpperCase(ch));
        } else {
            System.out.print(ch + " is not an alphabet character.\n");
        }
    }
}



2.4. Python Program for conversion between uppercase and lowercase letter using the methods upper() and lower()

Code has been copied
# ************************************************
# 	        alphabetacoder.com
# Python Program for Conversion Between Uppercase
# and Lowercase Letter using upper() and lower()
# ************************************************/

# take input
ch = input("Enter a character: ")

# check whether the input letter
# is in uppercase or lower case
# If the letter is in uppercase,
# convert it to lowercase
# If the character is in lowercase,
# convert it to uppercase
# If it is another symbol, keep
# it as it is
if ch >= "A" and ch <= "Z":
    print("It is an uppercase letter.")
    # display lowercase by using lower() method
    print("Lowercase of", ch, ":", ch.lower())
elif ch >= "a" and ch <= "z":
    print("It is an lowercase letter.")
    # display uppercase by using upper() method
    print("Uppercase of", ch, ":", ch.upper())
else:
    print(ch, "is not an alphabet character.")



2.5. C# Program for conversion between uppercase and lowercase letter using the functions ToUpper() and ToLower()

Code has been copied
/*************************************
		alphabetacoder.com
C# Program for Conversion Between 
Uppercase and Lowercase Letter
using ToUpper() and ToLower() methods
**************************************/

using System;

namespace UppercaseLowercase {
    class Program {
        static void Main(string[] args) {
            // declare variables
            char ch;

            // take input
            Console.Write("Enter a character: ");
            ch = Console.ReadLine()[0];

            //check whether the input letter
            // is in uppercase or lower case
            // If the letter is in uppercase,
            // convert it to lowercase
            // If the character is in lowercase,
            // convert it to uppercase
            // If it is another symbol, keep
            // it as it is
            if (ch >= 'A' && ch <= 'Z') {
                Console.WriteLine("It is an uppercase letter.");
                // display lowercase by using ToLower() method
                Console.WriteLine("Lowercase of " + ch + ": " + char.ToLower(ch));
            } else if (ch >= 'a' && ch <= 'z') {
                Console.WriteLine("It is an lowercase letter.");
                // display uppercase by using ToUpper() method
                Console.WriteLine("Uppercase of " + ch + ": " + char.ToUpper(ch));
            } else {
                Console.WriteLine(ch + " is not an alphabet character.");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}