isupper() in C
In C, isupper() is a standard library function that is used to determine if a given character is an uppercase letter. This function is defined in the ctype.h header file.
The isupper() function is supposed to take an integer argument which represents the value of the ASCII character to be checked. This function returns a non-zero integer value when the given character is an uppercase letter, and zero otherwise.
Here is the syntax of the isupper() function:
Syntax:
int isupper(int c);
The parameter c represents the integer value of the character to be checked.
In ASCII, the uppercase letters are represented by integers ranging from 65 to 90. Therefore, to check if a given character is an uppercase letter, we can use the isupper() function with the integer value of that character as its argument.
Here are some examples of how to use the isupper() function in C:
Example 1: Check if a character is an uppercase letter
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
if (isupper(ch)) {
printf("%c is an uppercase letter.", ch);
} else {
printf("%c is not an uppercase letter.", ch);
}
return 0;
}
Output:
A is an uppercase letter.
Example 2: Convert lowercase letters to uppercase
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "hello world";
int i = 0;
while (str[i]) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
}
i++;
}
printf("Uppercase string is: %s\n", str);
return 0;
}
Output:
Uppercase string is: HELLO WORLD
In this example, the islower() function is used to check if each character in the string str is a lowercase letter. When a lowercase letter is located, the toupper() function is used in order to convert the letters to uppercase. The returned uppercase string is printed to the console then .
Example 3: Count the number of uppercase letters in a string
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello World";
int i = 0, count = 0;
while (str[i]) {
if (isupper(str[i])) {
count++;
}
i++;
}
printf("Number of uppercase letters in the string is: %d\n", count);
return 0;
}
Output:
Number of uppercase letters in the string is: 2
In this example, the isupper() function is used to count the number of uppercase letters in the string str. For each character in the string, the isupper() function is called to check if the character is an uppercase letter. If a uppercase letter is found, the count variable is incremented. The final count of uppercase letters in the string is then printed to the console.
Here is an example to illustrate the difference between using isupper() and not usingit:
Without using isupper():
#include <stdio.h>
int main() {
char ch = 'A';
if (ch >= 'A' && ch <= 'Z') {
printf("%c is an uppercase letter.", ch);
} else {
printf("%c is not an uppercase letter.", ch);
}
return 0;
}
Output:
A is an uppercase letter.
In this example, the program checks if the character ch is an uppercase letter by using an if-statement with two conditions. The first condition checks if the character is greater than or equal to 'A', and the second condition checks if the character is less than or equal to 'Z'. If both conditions are true, it means that the character is an uppercase letter, and the program prints a message to the console stating that ch is an uppercase letter. If either condition is false, the program prints a message stating that ch is not an uppercase letter.
Using isupper():
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
if (isupper(ch)) {
printf("%c is an uppercase letter.", ch);
} else {
printf("%c is not an uppercase letter.", ch);
}
return 0;
}
Output:
A is an uppercase letter.
In this example, the program checks if the character ch is an uppercase letter by using the isupper() function. When isupper(ch) returns a non-zero value, it is considered that the character is an uppercase letter, and so the program prints a message to the console stating that ch is an uppercase letter and when the isupper(ch) function returns zero, the program prints the message stating that ch is not an uppercase letter.
As you can see, the second example is much simpler and easier to read, as it uses the isupper() function to check if the character is an uppercase letter, rather than a complex if-statement with multiple conditions.