Using the character class


Introduction

The character class provides static methods for evaluating and manipulating characters.


Analyzing Characters

Methods are provided for determining if a character is alphabetic, alphanumeric, lower case, upper case, etc.

#include <rudiments/character.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

        // print information about all ascii characters
        for (unsigned char uc=0; uc<128; uc++) {

                char    c=(char)uc;
                
                stdoutput.write("character ");
                stdoutput.safePrint(c);
                stdoutput.write(":\n");

                stdoutput.printf("      is alphanumeric         : %s\n",
                                (character::isAlphanumeric(c))?"yes":"no");
                stdoutput.printf("      is alphabetical         : %s\n",
                                (character::isAlphabetical(c))?"yes":"no");
                stdoutput.printf("      is lower case           : %s\n",
                                (character::isLowerCase(c))?"yes":"no");
                stdoutput.printf("      is upper case           : %s\n",
                                (character::isUpperCase(c))?"yes":"no");
                stdoutput.printf("      is punctuation          : %s\n",
                                (character::isPunctuation(c))?"yes":"no");
                stdoutput.printf("      is printable            : %s\n",
                                (character::isPrintable(c))?"yes":"no");
                stdoutput.printf("      is printable (non-space): %s\n",
                                (character::isPrintableNonSpace(c))?"yes":"no");
                stdoutput.printf("      is control chraracter   : %s\n",
                                (character::isControlCharacter(c))?"yes":"no");
                stdoutput.printf("      is digit                : %s\n",
                                (character::isDigit(c))?"yes":"no");
                stdoutput.printf("      is hex digit            : %s\n",
                                (character::isHexDigit(c))?"yes":"no");
                stdoutput.printf("      is blank                : %s\n",
                                (character::isBlank(c))?"yes":"no");
                stdoutput.printf("      is whitespace           : %s\n",
                                (character::isWhitespace(c))?"yes":"no");
                stdoutput.printf("      is ascii                : %s\n",
                                (character::isAscii(c))?"yes":"no");
        }
}

Transforming Characters

Methods are also provided for transforming the character to upper or lower case, and to 7-bit ascii, by stripping off the high order bit.

#include <rudiments/character.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

        // perform case conversions on all ascii characters
        for (unsigned char uc=0; uc<128; uc++) {

                char    c=(char)uc;
                
                stdoutput.write("character ");
                stdoutput.safePrint(c);
                stdoutput.write(":\n");

                stdoutput.write("       to upper case   : ");
                stdoutput.safePrint((char)character::toUpperCase(c));
                stdoutput.write("\n");

                stdoutput.write("       to lower case   : ");
                stdoutput.safePrint((char)character::toLowerCase(c));
                stdoutput.write("\n");

                // place a 1 in bit 8
                c=c|0x80;

                stdoutput.write("       to ascii        : ");
                stdoutput.safePrint((char)character::toAscii(c));
                stdoutput.write("\n");
        }
}

Member of a Set

An inSet() method is also provided which returns true if the specified character is a member of the specified set of characters and false otherwise.

#include <rudiments/character.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

        // define the set
        const char      set[]="abcdefg";

        // loop through the alphabet...
        for (char c='a'; c<='z'; c++) {
                stdoutput.printf("is %c is in the set \"%s\"?  %s\n",
                                c,set,
                                (character::inSet(c,set))?"yes":"no");
        }
}