Page 34 - C-Language
P. 34

size_t alnum;
           size_t punct;
         } chartypes;

         chartypes classify(const char *s) {
           chartypes types = { 0, 0, 0 };
           const char *p;
           for (p= s; p != '\0'; p++) {
             types.space += !!isspace((unsigned char)*p);
             types.alnum += !!isalnum((unsigned char)*p);
             types.punct += !!ispunct((unsigned char)*p);
           }

           return types;
         }


        The classify function examines all characters from a string and counts the number of spaces,
        alphanumeric and punctuation characters. It avoids several pitfalls.

            •  The character classification functions (e.g. isspace) expect their argument to be either
              representable as an unsigned char, or the value of the EOF macro.
            •  The expression *p is of type char and must therefore be converted to match the above
              wording.
            •  The char type is defined to be equivalent to either signed char or unsigned char.
            •  When char is equivalent to unsigned char, there is no problem, since every possible value of
              the char type is representable as unsigned char.
            •  When char is equivalent to signed char, it must be converted to unsigned char before being
              passed to the character classification functions. And although the value of the character may
              change because of this conversion, this is exactly what these functions expect.
            •  The return value of the character classification functions only distinguishes between zero
              (meaning false) and nonzero (meaning true). For counting the number of occurrences, this
              value needs to be converted to a 1 or 0, which is done by the double negation, !!.


        Introduction


        The header ctype.h is a part of the standard C library. It provides functions for classifying and
        converting characters.

        All of these functions take one parameter, an int that must be either EOF or representable as an
        unsigned char.


        The names of the classifying functions are prefixed with 'is'. Each returns an integer non-zero
        value (TRUE) if the character passed to it satisfies the related condition. If the condition is not
        satisfied then the function returns a zero value (FALSE).

        These classifying functions operate as shown, assuming the default C locale:


         int a;
         int c = 'A';
         a = isalpha(c); /* Checks if c is alphabetic (A-Z, a-z), returns non-zero here. */
         a = isalnum(c); /* Checks if c  is alphanumeric (A-Z, a-z, 0-9), returns non-zero here. */
         a = iscntrl(c); /* Checks is c is a control character (0x00-0x1F, 0x7F), returns zero here. */



        https://riptutorial.com/                                                                               10
   29   30   31   32   33   34   35   36   37   38   39