isupper() Function
The isupper()
function in C checks whether a given character is an uppercase letter. It is particularly useful for validating and processing textual data, ensuring that characters conform to expected casing rules.
Syntax of isupper()
int isupper(int c);
Parameters
Parameter | Description |
---|---|
c | An integer representing the character to be checked (typically cast from a char or EOF). |
Keep in mind that what is considered an uppercase letter may depend on the locale being used. In the default “C” locale, only the characters A through Z are considered uppercase. Other locales might define uppercase differently, but they will never include control characters, digits, punctuation, or whitespace.
Return Value
The function returns a nonzero value (true) if the character is an uppercase alphabetic letter, and zero (false) otherwise.
Examples for isupper()
Example 1: Simple Uppercase Check
This example demonstrates a basic usage of isupper()
to check if a single character is an uppercase letter.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = 'G';
if (isupper(ch)) {
printf("The character %c is uppercase.\n", ch);
} else {
printf("The character %c is not uppercase.\n", ch);
}
return 0;
}
Explanation:
- A character
'G'
is assigned to the variablech
. - The
isupper()
function checks if'G'
is an uppercase letter. - Since
'G'
is uppercase, the condition is true and the program prints the corresponding message.
Program Output:
The character G is uppercase.
Example 2: Checking Multiple Characters in a String
This example iterates over a string and uses isupper()
to check each character, indicating whether it is uppercase.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello World!";
int i = 0;
while (str[i] != '\0') {
if (isupper(str[i])) {
printf("Character '%c' is uppercase.\n", str[i]);
} else {
printf("Character '%c' is not uppercase.\n", str[i]);
}
i++;
}
return 0;
}
Explanation:
- A string
"Hello World!"
is defined. - The program iterates over each character in the string using a while loop.
- The
isupper()
function checks if the current character is an uppercase letter. - A message is printed for each character indicating whether it is uppercase or not.
Program Output:
Character 'H' is uppercase.
Character 'e' is not uppercase.
Character 'l' is not uppercase.
Character 'l' is not uppercase.
Character 'o' is not uppercase.
Character ' ' is not uppercase.
Character 'W' is uppercase.
Character 'o' is not uppercase.
Character 'r' is not uppercase.
Character 'l' is not uppercase.
Character 'd' is not uppercase.
Character '!' is not uppercase.
Example 3: Validating User Input for Uppercase Letters
This example demonstrates how to prompt the user for a character and check whether it is an uppercase letter using isupper()
.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char input;
printf("Enter a character: ");
scanf(" %c", &input);
if (isupper(input)) {
printf("The character %c is uppercase.\n", input);
} else {
printf("The character %c is not uppercase.\n", input);
}
return 0;
}
Explanation:
- The program prompts the user to enter a character.
- The entered character is stored in the variable
input
. - The
isupper()
function checks whether the provided character is uppercase. - A message is displayed indicating the result of the check.
Program Output:
Enter a character: G
The character G is uppercase.