Write a Program to convert a lowercase alphabet to uppercase or vice-versa using ASCII code

Posted in

Write a Program to convert a lowercase alphabet to uppercase or vice-versa using ASCII code
vinaykhatri

Vinay Khatri
Last updated on April 23, 2024

    ASCII stands for American Standard Code, and it represents a unique numeric value for every character. Every programing language provides some inbuilt functions to convert a lowercase character to uppercase and vice versa, but we can convert the case of a character without using those inbuilt functions or methods. In this article, we will write a program to convert a lowercase alphabet to uppercase or vice-versa using ASCII code . According to the ASCII code:

    A-Z = 65 to 90
    a-z = 97-122
    

    C Program to convert a lowercase alphabet to uppercase or vice-versa using ASCII code

    #include <stdio.h>
    
    int main() 
    {
       char alpha;
       printf("Enter an Aphabet: ");
       scanf("%c", &alpha);
       
       //if the alphabet is in Uppercase
       if(alpha>=65 && alpha <=90) 
       { 
            //convet it into uppper case 
            alpha +=32; 
            printf("The Alphabet has converted to Lowercase: %c",alpha); 
       } 
       
       //if the alphabet is in lowercase 
       else if(alpha>=97 && alpha <=122)
       {
       	//convert into uppercase
       	alpha -=32;
       	printf("The Alphabet has converted to Uppercase: %c ",alpha);
       }
       else
       {
       	printf("Please enter an alphabet [(a-z)(A-Z)]");
       }   
       return 0;
    }

    Output

    Enter an Alphabet: r
    The Alphabet has converted to Uppercase: R

    C++ Program to convert a lower case alphabet to uppercase or vice-versa using ASCII code

    #include <iostream>
    using namespace std;
    int main() 
    {	
     
       char alpha;
       cout<<"Enter an Alphabet: "; 
       cin>>alpha;
       
       //if the alphabet is in Uppercase
       //UpperCase Alphabets 
       //ASCII Code 65-90 (A to Z) 
       if(alpha>=65 && alpha <=90)
       {
       	//convet it into uppper case
       	alpha +=32;
       	cout<<"The Alphabet has converted to Lowercase: "<<alpha; 
       } 
      //if the alphabet is in lowercase 
      //lowercase Alphabets 
      //ASCII code 97-122 [A-Z] 
      else if(alpha>=97 && alpha <=122)
       {
       	//convert into uppercase
       	alpha -=32;
       	cout<<"The Alphabet has converted to Uppercase: "<<alpha;
       }
       else
       {
       	cout<<"Please enter an alphabet [(a-z)(A-Z)]";
       }
       return 0;
    }

    Output

    Enter an Alphabet: r
    The Alphabet has converted to Uppercase: R

    Python Program to convert a lowercase alphabet to uppercase or vice-versa using ASCII code

    alpha = input("Enter An Alphabet: ")
    
    alpha_ascii = ord(alpha)
    
    # if the alphabet is in Uppercase
    # UpperCase Alphabets 
    # ASCII Code 65-90 (A to Z) 
    if(alpha_ascii>=65 and alpha_ascii<=90):
        # convet it into uppper case
        alpha_ascii+=32
        alpha = chr(alpha_ascii)
        print("The Alphabet has converted to Lowercase:", alpha)
    
    # if the alphabet is in lowercase
    # lowercase Alphabets
    # ASCII code 97-122 [A-Z]
    elif(alpha_ascii>=97 and alpha_ascii<=122):
        alpha_ascii-=32
        alpha = chr(alpha_ascii)
        print("The Alphabet has converted to Uppercase:",alpha)
    
    else:
        print("Please enter an alphabet [(a-z)(A-Z)]")

    Output

    Enter An Alphabet: z
    The Alphabet has converted to Uppercase: Z

    Wrapping Up

    In this article, we write a program in C, C++, and Python to convert a lowercase alphabet to uppercase or vice-versa using ASCII code. The ASCII code for Uppercase alphabets starts from 65 up to 90 and for lower case, it starts from 97 up to 122. Using the upper program we could also tell if the user entered alphabet is a lower case or upper case letter.

    People are also reading:

    Leave a Comment on this Post

    0 Comments