C Program to Convert Character to Uppercase

How to write a C Program to convert character to uppercase using the built-in function toupper, and also by not using toupper function. The C Programming toupper is a built-in function present in the <ctype.h> header file. It is to convert the lowercase character to uppercase in C. The Syntax of the C toupper function is

toupper (<character>);

C Program to Convert Character to Uppercase using toupper function

This program to convert lowercase characters to uppercase characters allows the user to enter any character. Next, it will convert the character to uppercase using the built-in ctype function called toupper.

#include <stdio.h> 
#include <ctype.h>

int main()
{
  char Ch;
 
  printf("\n Please Enter any alphabet\n");
  scanf(" %c", &Ch);

  if (isalpha(Ch) )
   {  
     Ch = toupper(Ch); 
     printf ("\n Uppercase of Entered character is %c", Ch);
   }
  else
   {
     printf("\n Please Enter Valid Alphabet");
   }  
}
C Program to Convert Character to Uppercase 1

In this program to convert lowercase character to uppercase character in C, we declared a character variable Ch. In the next line, we used the If Statement, if (isalpha(Ch)).

If the condition is TRUE, the given character is an Alphabet. Next, we can convert the given character to uppercase using below c topper function toupper(Ch).

In the next line, we have the C programming printf statement to print the converted uppercase letter.

If the above condition is FALSE, the given character is not Alphabet. So, it will print the else statement

C Program to Convert Character to Uppercase without using toupper function

This C program allows the user to enter any character and convert the character to uppercase without using a built-in ctype function toupper.

In this example, we are going to use the ASCII code to convert lowercase character to uppercase. Please refer ASCII Value of a Character article to know the ASCII values of every letter.

#include <stdio.h> 

int main()
{
  char Ch;
 
  printf("\n Please Enter any alphabet\n");
  scanf(" %c", &Ch);
  
  if (isalpha(Ch) )
  {
   if (Ch>=97 && Ch<=122)
    {  
      Ch = Ch-32; 
      printf ("\n Uppercase of Entered character is %c", Ch);
    }
   else
    {
      printf("\n You Already Entered Uppercase Character");
    }  
  }
  else
   {
     printf("\n Entered character is Not an Alphabet");
   }  
}
 Please Enter any alphabet
a

 Uppercase of Entered character is A

Let us see, what happens when we assign Uppercase letter to Ch

 Please Enter any alphabet
L

 You Already Entered Uppercase Character

Let us see, what happens when we assign Digit to Ch

 Please Enter any alphabet
9

 Entered character is Not an Alphabet

Within this C Program to Convert Character to Uppercase example, If you look at the above Nested If Statement

if (Ch>=97 && Ch<=122)
//or
if (Ch >= 'a' && Ch <= 'z')

As we all know that, all the lowercase characters are between a and z, and their ASCII codes are 97 to 122. So, the above if condition checks whether the given character is between a and z.

If the above condition is TRUE, then we have to convert the given character to upper case. If you see the ASCII codes of uppercase and lowercase characters, the difference between them is 32. For instance ASCII code for a = 97 & A = 65 (Difference is 32). That’s why we subtracted 32 from the character ASCII value. In the next line, we have the printf statement to print the converted uppercase letter

If the above condition is FALSE, the given character is already in Upper case So, it will print the below statement