How to use isupper function in C programming?

The isupper function in C programming checks whether the argument passed is an uppercase letter or not. In the “C” locale uppercase letters are any of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

It is declared in ctype.h and takes one argument in the form of integer and returns the value of type int. If isupper returns a nonzero value, it is guaranteed that iscntrl, isdigit, ispunct, and isspace return zero for the same character in the same locale.

 

Syntax of isupper function in C:

//Syntax of isupper

int isupper(int c);

Parameters:

c => character to classify

Return value:

Non-zero value => If the argument is an uppercase letter.
0 => If the argument is neither an uppercase letter.

 

Example,

Input : A
Output : Non-zero value

Input : a
Output : zero

Input : 1
Output : Zero

Input : @
Output : Zero

Input : B
Output : Non-zero value

 

 

C Program to Check whether a Character Entered by User is an uppercase letter or not Using the isupper:

Let’s see a C program to check given character is an uppercase letter or not.

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

int main()
{
    int c;
    printf("Enter a Valid Character: ");
    c = fgetc(stdin);
    if (isupper(c) == 0)
    {
        printf("%c is not a uppercase letter.", c);
    }
    else
    {
        printf("%c is a uppercase letter.", c);
    }
    return 0;
}

Output1:

Enter a character: @
@ is not an uppercase letter.

 

Output2:

Enter a character: a
a is not an uppercase letter.

Output3:

Enter a character: A
A is an uppercase letter.

 

Print Count of the uppercase letter in given input string:

There are many applications of isupper in C programming. But to find out the count of uppercase characters in a given input stream is very popular. So let’s see a C code to find the count of the uppercase characters in the given input stream.

Algorithm:

1. Traverse the given string character by character and passed it into the isupper function.

2. Increment the counter variable whenever encounter the uppercase letter.

3. Break the loop when encountered the null character (Limitation there must not be another null character in the string except the terminating null character).

4. Return the value of the counter from the function and print the value in the main function.

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

unsigned int findCountUpperCaseLetterGivenStream(char *str)
{
    unsigned int counter = 0;
    if (str != NULL)
    {
        unsigned int i = 0;
        // counting of control char
        while (str[i] != '\0')
        {
            if (isupper((unsigned char)str[i]))
            {
                ++counter;
            }
            ++i;
        }
    }
    // returning total number of uppercase char
    // present in given input stream
    return (counter);
}


int main()
{
    char str[] = "aticleWorlD";

    unsigned int counter = findCountUpperCaseLetterGivenStream(str);

    printf("Total number of char in input stream is : %u\n\n", strlen(str));

    printf("\nNumber of uppercase char in the "
           "given input stream is : %u\n\n", counter);

    return 0;
}

Output:

Total number of char in input stream is : 11

Number of uppercase char in the given input stream is : 2

 

Print the string till the first uppercase letter is encountered:

Another popular program is to print given a string till the first uppercase character is encountered. With the help of isupper(), we can easily do it. So let’s see a small program.

Algorithm:

1. Traverse the given string character by character and passed it into the isupper function.

2. If the character is not an uppercase character it returns zero. You can see in the while loop braces, I have used logical Not ( ! ) with isupper(). So for each non-uppercase letter loop will run and print the character on stdout.

3. When the uppercase character will encounter the while loop will break.

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

int main()
{
    char str[] = "aticle_C_TUTORIALworld";
    unsigned int i = 0;

    // printing till first uppercase char
    while (!isupper((unsigned char)str[i]))
    {
        putchar(str[i]);
        ++i;
    }
    return 0;
}

Output:

aticle_

 

First uppercase letter in a string (Iterative and Recursive):

It is another interesting application of an isupper function to find the first occurrence of the uppercase letter in a given input string. We will see it using both ways iterative and recursive.

Example,

Input : aticleWorld
Output : W


Input  : abDa
Output : D

 

Iterative Way:

Using the linear search we will find the first uppercase character with the help of isupper().

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


char firstUpperCaseCharGivenStream(char *str)
{
    unsigned int i = 0;
    char firstUpperCaseChar = 0;
    if (str != NULL)
    {
        // find first upperrcase char
        while (str[i] != '\0')
        {
            if (isupper((unsigned char)str[i]))
            {
                firstUpperCaseChar = str[i];
                break;
            }
            ++i;
        }
    }
    // returning first uppercase char
    // present in given input stream
    return (firstUpperCaseChar);
}

int main()
{
    //String must have only one null char (terminating null)
    char str[] = "aticleWorld.com";

    unsigned int firstUpperCaseChar = firstUpperCaseCharGivenStream(str);

    if(firstUpperCaseChar != 0)
    {
        printf("Total number of char in input stream is : %u\n\n", strlen(str));
        printf("\nFirst uppercase char in the "
               "given input stream is : %c \n\n", firstUpperCaseChar);
    }

    return 0;
}

Output:

isupper function in C

 

Recursive Way:

Recursively traverse the string and if any uppercase is found return that character.

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

char firstUpperCaseCharGivenStream(char * str, int i)
{
    if (str != NULL)
    {
        if (str[i] == '\0')
        {
            return 0;
        }
        if (isupper(str[i]))
        {
            return str[i];
        }
    }
    return firstUpperCaseCharGivenStream(str, i+1);
}

int main()
{
    //String must have only one null char (terminating null)
    char str[] = "aticleWorld.com";

    unsigned int firstUpperCaseChar = firstUpperCaseCharGivenStream(str, 0);

    if(firstUpperCaseChar != 0)
    {
        printf("Total number of char in input stream is : %u\n\n", strlen(str));
        printf("\nFirst uppercase char in the "
               "given input stream is : %c \n\n", firstUpperCaseChar);
    }

    return 0;
}

Output:

isupper function in C

 

Note: If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of isupper is undefined.

 

As we know the behavior of isupper is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.

int my_isupper(char ch)
{
    return isupper((unsigned char)ch);
}

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *