Tutorial Gateway

  • C Programming
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • About
    • About Us
    • Contact Us

C Program to Convert Character to Uppercase

30-04-2015 by suresh Leave a Comment

In this article we will show you, How to write a C Program to convert character to uppercase using the built in function toupper, and also by not using toupper function.

toupper Function

In C Programming, toupper is a built-in function present in the <ctype.h> header file which is used to convert the character to uppercase. The Syntax of the toupper function is

toupper (<character>);

C Program to Convert Character to Uppercase using toupper function

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

CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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");
   }  
}

OUTPUT

C Program to Convert Character to Uppercase 1

ANALYSIS
In this program we declared a character variable Ch. Below printf statement will asked to enter any character you like.

1
printf("\n Please Enter any character \n");

Below scanf statement will assign the user entered character to Ch variable

1
scanf("%c", &Ch);

In the next line we used the If Statement,

1
if (isalpha(Ch))

If the above condition is TRUE then the given character is an Alphabet and now we can convert the given character to uppercase using below statement

1
Ch = toupper(Ch);

In the next line we have the printf statement to print the converted uppercase letter

1
printf ("\n Uppercase of Entered character is %c", Ch);

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

1
printf("\n Please Enter Valid Alphabet");

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 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 understand the ASCII values of every character.

CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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");
   }  
}

OUTPUT
C Program to Convert Character to Uppercase Without using toupper 1

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

C Program to Convert Character to Uppercase Without using toupper 2

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

C Program to Convert Character to Uppercase Without using toupper 3

ANALYSIS

If you look at the above Nested If Statement

1
2
3
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 will check whether the given character is between a and z.
If the above condition is TRUE then we have to convert the given character to uppercase. If you observe 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) thats why we subtracted 32 from the character ASCII value.

1
Ch = Ch - 32;

In the next line we have the printf statement to print the converted uppercase letter

1
printf ("\n Uppercase of Entered character is %c", Ch);

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

1
printf("\n You Already Entered Uppercase Character");

Thank you for Visiting Our Blog

Placed Under: C Programs

Share your Feedback, or Code!! Cancel reply

Trending

  • SQL Server Integration Services (SSIS)
  • SQL Server Tutorial
  • SQL Server Reporting Services (SSRS)
  • Home
  • C Program to Calculate the Sum and Average of n Number
  • Tableau
  • C Program to Print Prime Numbers from 1 to 100

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • Blogger
  • C Programs
  • Java Programs
  • SQL
  • SSIS
  • SSRS
  • Tableau
  • JavaScript

Copyright © 2018 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy