C program to check whether a character is Uppercase or Lowercase

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.

C program to read a character from user and check whether the character is uppercase or lowercase using inbuilt library functions.

Example:
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 program to check alphabet, we have already checked for both uppercase and lowercase alphabets if you remember. Here also we are going to use same logic. To check a character is uppercase or lowercase you just need to check whether the given characters lies in a certain range or not.
For uppercase characters check whether it lies between A-Z
And for lowercase check whether it lies between a-z

Since characters in C are internally represented as ASCII values you can also use their ASCII character codes to check whether a character is uppercase or lowercase.

Program to check uppercase or lowercase characters

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

#include <stdio.h>

int main()
{
    char ch;

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


    if(ch >= 'A' && ch <= 'Z')
    {
        printf("%c is uppercase alphabet.\n", ch);
    }
    else if(ch >= 'a' && ch <= 'z')
    {
        printf("%c is lowercase alphabet.\n", ch);
    }
    else
    {
        printf("%c is not an alphabet.\n", 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.\n", ch);
    }
    else if(islower(ch))
    {
        printf("%c is lowercase alphabet.\n", ch);
    }
    else
    {
        printf("%c is not an alphabet.\n", ch);
    }

    return 0;
} 


Output
Enter any character: C
C is uppercase alphabet.

Happy coding ;)


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
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment