C program to check total number of uppercase letters

C has a standard library or header file called ctype.h that contains library functions like isupper(), islower(), etc for checking the case of letters of a given string or character. So in this program, our task is to check total number of uppercase characters of a given string. To do that, we will use the isupper() function from the ctype.h library.

You can also use the manual method to calculate the total number of uppercase letters. But using library function, you can do it very easily with a very precise code.

isupper() returning value

When isupper() function is called with a single alphabetic character as a function parameter, the function return the boolean value 1 or 0 that indicates correspondingly true or false. Check the code below, how it returns true or false;

#include <stdio.h>
#include <ctype.h> //this header includes isupper() function

//isUpper function to check letter case
int isUpper(){
    char ch = 'A'; //'A' is uppercase thats why returns 1 or true
    return isupper(ch); //returns 1 or true to main function
}

//the main function
int main(void) {
    printf("%d", isUpper() );
}

Output

Sample InputSample Output
No input1

Counting total numbers of uppercase letters

We can use a loop in our program to count the number of uppercase letters using the isupper() function. As we saw before, isupper() returns a boolean value true or false, so that we can use condition for counting. To do this we will take a counter variable and then we will check the string character by character using the loop.

Every time the isupper() function will return 1 or evaluates to true then we will increment the counter by one. Finally, we will get the total number of uppercase letters from the counter variable. Here, how can we do that.

C program to check total number of uppercase letters

#include <stdio.h>
#include <ctype.h> //this header includes isupper() function

int main() {
    char name[25];
    int i, counter=0;

    printf("Enter your name: ");
    gets(name);

    for(i=0; name[i]!='\0'; i++){
        if (isupper(name[i]))
            counter++;
    }

    printf("Total number of uppercase letters: %d\n", counter);
    
    return 0;
}

Output

Sample InputSample Output
Enter your name: Mehrab MohulTotal number of uppercase letters: 2
Enter your name: AbCdeFGhTotal number of uppercase letters: 4
Posted in