C Library - isupper() function



The C ctype library isupper() function is used to check if a given character is an uppercase letter or not

Syntax

Following is the C library syntax of the isupper() function −

int isupper(int c);

Parameters

This function accepts a single parameter −

  • c − This is the character to be checked, passed as an int. The value of c should be representable as an unsigned char or be equal to the value of EOF.

Return Value

The isupper function returns a non-zero value (true) if the character is an uppercase letter (from 'A' to 'Z'). Otherwise, it returns 0 (false).

Example 1: Checking a Single Character

Here, the character 'G' is checked using isupper, and since it is an uppercase letter, the function returns a non-zero value.

#include <stdio.h>
#include <ctype.h>
int main() {
    char ch = 'G';
    if (isupper(ch)) {
        printf("'%c' is an uppercase letter.\n", ch);
    } else {
        printf("'%c' is not an uppercase letter.\n", ch);
    }
    return 0;
}

Output

After execution of above code, we get the following result

'G' is an uppercase letter.

Example 2: Iterating Through a String

This example iterates through the string "Hello World!" and counts the number of uppercase letters.

#include <stdio.h>
#include <ctype.h>

int main() {
   char str[] = "Hello World!";
   int uppercase_count = 0;

   for (int i = 0; str[i] != '\0'; i++) {
      if (isupper(str[i])) {
         uppercase_count++;
      }
   }
   printf("The string \"%s\" has %d uppercase letters.\n", str, uppercase_count);
   return 0;
}

Output

The output of the above code is as follows −

The string "Hello World!" has 2 uppercase letters.
Advertisements