C program to check whether a character is Uppercase or Lowercase

Previous Program Next Program

Write a C program to check whether a character is uppercase or lowercase alphabet using if else . How to check uppercase and lowercase using if else in C programming. Program to enter a character from user and check whether the character is uppercase or lowercase alphabet without using in built library function. Logic to check uppercase and lowercase alphabets in C program.

Example

Input

Input character: C

Output

C is uppercase alphabet

Required knowledge

Basic C programming, If else

Logic to check uppercase and lowercase alphabets

If you are following my previous posts then you probably know the logic of this program. In previous posts I already explained logic for lower and uppercase alphabets. Let us recall the definitions for lowercase and uppercase alphabets once again.

  • Lowercase alphabets are the characters in between a-z (ASCII 97-122).
  • Uppercase alphabets are the character in between A-Z (ASCII 65-90).

Read the list of ASCII character values.

Using the above definition we can write a step by step descriptive logic -

  1. Read a character from user and store it in some variable say ch.
  2. Check condition for uppercase alphabets i.e. if ch >= 'A' and ch <= 'Z'. Then the given character is uppercase alphabet.
  3. Check condition for lowercase alphabets i.e. if ch >= 'a' and ch <= 'z'. Then the given character is lowercase alphabet.

Let us code the solution for this program.

Program to check uppercase or lowercase alphabets

/**
 * C program to check whether a character is uppercase or lowercase 
 */

#include <stdio.h>

int main()
{
    char ch;

    /* Read character from user */
    printf("Enter any character: ");
    scanf("%c", &ch);


    if(ch >= 'A' && ch <= 'Z')
    {
        printf("%c is uppercase alphabet.", ch);
    }
    else if(ch >= 'a' && ch <= 'z')
    {
        printf("%c is lowercase alphabet.", ch);
    }
    else
    {
        printf("%c is not an alphabet.", ch);
    }

    return 0;
} 

Note: You can also use inbuilt library function isupper() and islower() to check whether a character is uppercase or lowercase respectively. These functions are present under ctype.h header file. Both the functions returns 1 if the given character is uppercase or lowercase respectively otherwise returns 0.

Program to check uppercase or lowercase characters using library functions

/**
 * C program to check whether a character is uppercase 
 * or lowercase using inbuilt library functions
 */

#include <stdio.h>
#include <ctype.h> //Used for isupper() and islower()

int main()
{
    char ch;

    /* Reads a character from user */
    printf("Enter any character: ");
    scanf("%c", &ch);

    if(isupper(ch))
    {
        printf("%c is uppercase alphabet.", ch);
    }
    else if(islower(ch))
    {
        printf("%c is lowercase alphabet.", ch);
    }
    else
    {
        printf("%c is not an alphabet.", ch);
    }

    return 0;
} 
Output
Enter any character: C
C is uppercase alphabet.

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment