Rewrite C Function: ispunct

Last Updated : 10 July, 2022 · 2 min read

The goal of this tutorial is to closely investigate the ispunct function in the C programming language. This tutorial is divided into four relevant parts:

Structure of ispunct:

Declaration: int ispunct(int c);

Return: non-zero value if int c is a punctuation 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 ispunct function in C?

The ispunct function checks to determine if a value is a punctuation character. Punctuation character being a mark that is used to either separate written language elements or to clarify meaning. These punctuation marks fall on 4 different ranges on the ASCII table: [33, 47], [58, 64], [91, 96] and [123, 126].

After receiving a value, the ispunct function checks if it can be found in one of the four ranges and returns a non-zero value, if a value can, in fact, be found. If not, then zero is returned from this function, indicating that the received value is not a punctuation mark.

Example 1: Usage of ispunct function in C.

# include <stdio.h>
# include <ctype.h>
int main ()
{
    
char * str = "Take a quiz :)" ;
    
int i = 0, total = 0;
    
while (str[i] != '\0' )
    
{
        
if ( ispunct (str[i]))
        
{
            
total++;
            
printf ( "ispunct: %c\n" , str[i]);
        
}
        
i++;
    
}
    
printf ( "Total: %d" , total);
    
return (0);
}
                    

Possible output:

ispunct: :
ispunct: )
Total: 2

2. How to rewrite the ispunct function in C?

Now, in order to rewrite or to implement the ispunct function we need to check if the received parameter happens to fall on one of the mentioned ranges. For this purpose we can make use of other ctype.h library functions, that is isdigit, isalpha, iscntrl and isgraph. Below, you can find a code example as to how to achieve this.

Example 2: rewritten ispunct function in C.

int our_ispunct ( int c)
{
    
if ( !isdigit (c) && !isalpha (c) && !iscntrl (c) && isgraph (c))
    
{
        
return 1;
    
}
    
return 0;
}
                    

The conditional statement (on line 3, example 2) returns true if a received parameter c is a punctuation mark. In which case the our_ispunct function returns 1.

The conditional statement itself may appear to be a bit obscure. What it is checking, in essence, is whether the received parameter is not a numeric, alphabetic nor a control value, and that it is a value that has a graphic representation. Now, only punctuation marks satisfy such condition.

3. Testing the rewritten ispunct function

To test the our_ispunct function we can use the main function below.

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

# include <stdio.h>
# include <ctype.h>
int our_ispunct ( int c)
{
    
if ( !isdigit (c) && !isalpha (c) && !iscntrl (c) && isgraph (c))
    
{
        
return 1;
    
}
    
return 0;
}
                    
int main ()
{
    
int i = 0;
    
int is_equal = 1;
    
while (i <= 127)
    
{
        
if ( ispunct (i) > 0 != our_ispunct (i) > 0)
        
{
            
printf ( "Functions behave differently, when i == (%d)\n" , i);
            
is_equal = 0;
        
}
        
i++;
    
}
    
if (is_equal)
    
{
        
printf ( "Functions behave the same way!\n" );
    
}
    
return (0);
}
                    

Possible output:

Functions behave the same way!

In the main function (example 3), we are looping through all the numbers from 0 to 127 included. For each number, we are comparing results of both ispunct and our_ispunct functions (on line 17, example 3). If the results differ, we print a message (on line 19, example 3) and set the variable is_equal to 0 (on line 20, example 3). After iterating through all the numbers, we check the value of the variable is_equal (on line 24, example 3). Only in the case when results of both functions matched with every single digit, we get to see "Functions behave the same way!" printed to the standard output.

4. Quiz of the reader's knowledge

Quiz : Rewrite C Function (ispunct)

1What is the return value of ispunct(NULL)?

2What is the return data type of ispunct?

2In which library is ispunct defined?

quiz completed!

Congrats!

Feel free to share your achievement below!