Check if a character is a punctuation mark in Arduino


Just like there is a function to check if a character is alphanumeric or not, there is another one to check if a character is a punctuation mark or not. The name of the function is isPunct(). It takes a character as an input and returns a Boolean: true if the given character is a punctuation mark.

Example

The following example demonstrates the use of this function −

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   Serial.println();
   char c1 = 'a';
   char c2 = ',';
   char c3 = '1';
   char c4 = '$';
   char c5 = '%';

   if (isPunct(c1)) {
      Serial.println("c1 is a punctuation mark!");
   } else {
      Serial.println("c1 is NOT a punctuation mark!");
   }

   if (isPunct(c2)) {
      Serial.println("c2 is a punctuation mark!");
   } else {
      Serial.println("c2 is NOT a punctuation mark!");
   }

   if (isPunct(c3)) {
      Serial.println("c3 is a punctuation mark!");
   } else {
      Serial.println("c3 is NOT a punctuation mark!");
   }

   if (isPunct(c4)) {
      Serial.println("c4 is a punctuation mark!");
   } else {
      Serial.println("c4 is NOT a punctuation mark!");
   }

   if (isPunct(c5)) {
      Serial.println("c5 is a punctuation mark!");
   } else {
      Serial.println("c5 is NOT a punctuation mark!");
   }
}

void loop() {
   // put your main code here, to run repeatedly:
}

Output

The Serial Monitor output is shown below −

While it is a bit unexpected, even characters like ‘$’ and ‘%’ have been recognized as punctuation marks by this function. It appears that this function treats all special characters as punctuation marks. You are encouraged to try other special characters with this function.

Updated on: 31-May-2021

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements