C Programming

C Check Is Uppercase

If a character uses a capital letter, it would seem to be in uppercase, and when the character uses a small letter, it appears to be in lowercase. Uppercase letters range from “A” to “Z”, and their ASCII codes range from 65 to 90. The lowercase letters range from “a” to “z”, and they have ASCII codes ranging from 97 to 122. In C language, the term “char” is utilized to declare the variables with a character data type. In this article, we’ll look at how to use the C language to determine whether a defined character is in uppercase or lowercase.

Use Alphabets to Identify If the Required Character Is Uppercase or Lowercase

The technique for determining if an inserted letter is in lowercase or uppercase in a C language would be to compare it to the alphabets themselves. It is depicted below:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char chr;
    printf("Enter a character: ");
     scanf("%C",&chr);
     if(chr>='A' && chr='a' && chr<='z'){
    printf("%c is a lower case character ",chr);
}
else{
    printf("%c is not a Alphabet ",chr);
}
    return 0;
}

In this example, we start the program by including the header files <stdio.h> and <stdlib.h>. Then, we call the main() function. Within the main() function, we have to declare a variable that has a character data type. Now, we want to take the character from the user, so we utilize the printf() function. Through this, the statement is displayed on the screen, and the user inputs the character of his own choice. The entered character may be a lowercase character or uppercase character. That character would be stored in the “chr” variable using the scanf() method.

In addition, we apply the if-else-if statement. Here, we set the condition that if the entered character is greater than or equal to “A” and less than or equal to “Z” the printf() function prints that the defined character is the upper case character. And whenever this condition becomes false. Further, we apply the else-if statement and evaluate the condition.

Here, we specify that if the entered letter is >= “a” and <= equal to “z” the entered cheater should be a lowercase character. If this defined condition is not true, we go to the else statement. When the entered character is not the uppercase or lowercase, then it is not even an alphabet. To show this on screen, we utilize the printf() function. In the end, the return 0 command is applied.

Use the ASCII Code to Determine If the Defined Character Exists in Uppercase or Lowercase

The lowercase character “a” has an ASCII code of 97, “b” has an ASCII code of 98, and so on. The uppercase character “A” has an ASCII code of 65, “B” has an ASCII code of 66, and so on. Here, the program validates the ASCII code of the provided character to see if it is lowercase or uppercase.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char chr;
    printf("Enter a character: ");
     scanf("%C",&chr);
     if(chr>=65 && chr=97 && chr<=122){
    printf("%c is a lower case character ",chr);
}
else{
    printf("%c is not a Alphabets ",chr);
}
    return 0;
}

First, we introduce two required libraries. After this, we start the coding in the body of the main() function. Here, we create a variable called char “chr”. Then, we ask the user to insert any letter to see whether it is uppercase or lowercase using the printf() function. In addition to this, we employ the scanf() method, which stores the provided character. We use if-else-if statements to analyze whether the given letter is uppercase. Here, we apply test expressions.

First, we have been utilizing the if statement to check uppercase. If the test condition evaluates to true, the evaluated character is upper case. Whenever this if-statement is untrue, the control shifts to the else if and analyzes the else-if test condition. The evaluated letter is the lowercase if the else-if test statement is true. When the else-if test condition is false, control is passed to the else portion, which implements the else portion declaration.

In this, we decide that the entered letter is uppercase or lowercase. To terminate the code, we utilize the return 0 command:

Use the isupper() Method to Determine If the Defined Character Is in Uppercase or Lowercase

The isupper() method in C language determines whether or not a specified letter is uppercase. The isupper() method indicates if the entered character will be in uppercase according to the existing C locale categorization. If the value of entered character cannot be expressed with an unsigned char and is therefore not similar to EOF, the outcome of isupper() is unspecified. The isupper() function is declared in the header file <ctype.h>. If the argument provided is an uppercase letter, the isupper() method returns 1, but if the argument given is a lowercase letter, it returns 0.

Here, we are going to integrate two header files <stdio.h> and <cstype.h>. The library <cstype.h> handles the use of the isupper() method. In the next step, we call the main() function. Further, we define a variable “ch” to store the character. Then, we use the printf() function to display the statements.

Similarly, we take the letter from the user. Here, we utilize the printf() method. The scanf() function is called to store the entered letter. In addition, we employ the isupper() function to check if the defined character or letter is uppercase or lowercase. The isupper() function contains the provided character as a parameter.

Meanwhile, we apply the if-else condition here. We use the return 0 statement to terminate the program.

Conclusion

This article discussed the techniques used to check whether the provided letter is uppercase or lowercase. We evaluate three approaches, including the use of alphabets, the use of ASCII codes, and the use of the isupper() method to check the uppercase letters. Check other Linux Hint articles for more tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content