Rewrite C Function: islower

Last Updated : 10 July, 2022 · 2 min read

articles banner image

In this tutorial we will cover the islower function in C programming language. Here you can expect to find:

Structure of islower:

Declaration: int islower(int c);

Return: a non-zero value if int c is a lowercase alphabetic value. Otherwise, 0.

Parameter list:
(int c) - the character that is checked to determine the return value.

Header file: <ctype.h>.

1. So, what is the islower function in C?

The islower function in C programming language checks the input value to determine if it is a lowercase character. The lowercase characters in C have ASCII values in the range of [97, 122]. Meaning that if the input value is more or equal to 97 and less or equal to 122, then it is a lowercase character. In such a case, the islower function returns a non-zero value. If the input value is outside of the mentioned range, then 0 is return.

Example 1: Usage example of the islower function in C.

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

int main( void )
{
    
printf ( "1. %d\n", islower ( 'a' ));
    
printf ( "2. %d", islower ( 'A' ));
    
return ( 0 );
}

Possible output:

1. 1
2. 0

2. How to rewrite the islower function in C?

Now, to implement the islower function accurately, we need to satisfy the following three conditions:

Example 2: Rewritten islower function in C.

int our_islower ( int c )
{
    
if ( c >= 97 && c <= 122 )
        
return ( 1 );
    
return ( 0 );
}

You can see (on line 3) a conditional statement, which checks if the c value is within the lowercase character range. It returns true, when the received parameter is a lowercase character and so 1 will be returned (on line 4). If this conditional statement returns false, then a zero will be returned (on line 5).

3. Testing the rewritten islower function

Now that we have our_islower function implemented, it is a good time to test it. For that we are going to compare the return values of our_islower function with the original standard library function with a couple of values.

Example 3: Test of the rewritten islower function in C.

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

int our_islower ( int c )
{
    
if ( c >= 97 && c <= 122 )
        
return ( 1 );
    
return ( 0 );
}
int main( void )
{
    
printf ( "1. our_islower: %d, islower: %d\n" , our_islower ( 'a' ), islower ( 'a' ));
    
printf ( "2. our_islower: %d, islower: %d\n" , our_islower ( 'A' ), islower ( 'A' ));
    
return ( 0 );
}

Possible output:

1. our_islower: 1, islower: 1
2. our_islower: 0, islower: 0

Since we have matching return values, we can assume accuracy of our implementation of the islower function.

Note: The islower function can return various non-zero values on different compilers.

Congratulations! You have reached the last part of this tutorial. Now you are ready to take the quiz!

4. Quiz of the islower function

Quiz : Rewrite C Function (islower)

1Which of the following condition returns true with all lowercase characters?

2In which library is the islower function defined?

2What is the return value of islower(97)?

quiz completed!

Congrats!

Feel free to share your achievement below!