training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

myctype.c (3985B)


      1 /* Exercise 8.26 */
      2 
      3 #include <stdio.h>
      4 
      5 int isdigit(int c);
      6 int isalpha(int c);
      7 int isalnum(int c);
      8 int isxdigit(int c);
      9 int islower(int c);
     10 int isupper(int c);
     11 int tolower(int c);
     12 int toupper(int c);
     13 int isspace(int c);
     14 int iscntrl(int c);
     15 int ispunct(int c);
     16 int isprint(int c);
     17 int isgraph(int c);
     18 
     19 int main(void)
     20 {
     21    char c;
     22 
     23    printf("Give me a char: ");
     24    scanf("%c", &c);
     25 
     26    printf("The char %c (%d) %s %s\n", c, c,
     27       isdigit(c) ? "is" : "is not", "a digit");
     28 
     29    printf("The char %c (%d) %s %s\n", c, c,
     30       isalpha(c) ? "is" : "is not", "alphabetic");
     31 
     32    printf("The char %c (%d) %s %s\n", c, c,
     33       isalnum(c) ? "is" : "is not", "alphanumeric");
     34 
     35    printf("The char %c (%d) %s %s\n", c, c,
     36       isxdigit(c) ? "is" : "is not", "hexadecimal");
     37 
     38    printf("The char %c (%d) %s %s\n", c, c,
     39       islower(c) ? "is" : "is not", "lower");
     40 
     41    printf("The char %c (%d) %s %s\n", c, c,
     42       isupper(c) ? "is" : "is not", "upper");
     43 
     44    printf("The char %c (%d) to lower is %c\n", c, c, tolower(c));
     45    printf("The char %c (%d) to upper is %c\n", c, c, toupper(c));
     46 
     47    printf("The char %c (%d) %s %s\n", c, c,
     48       iscntrl(c) ? "is" : "is not", "of control");
     49 
     50    printf("The char %c (%d) %s %s\n", c, c,
     51       ispunct(c) ? "is" : "is not", "a punctuation");
     52 
     53    printf("The char %c (%d) %s %s\n", c, c,
     54       isprint(c) ? "is" : "is not", "printing");
     55 
     56    printf("The char %c (%d) %s %s\n", c, c,
     57       isspace(c) ? "is" : "is not", "a space");
     58 
     59    printf("The char %c (%d) %s %s\n", c, c,
     60       isgraph(c) ? "is" : "is not", "a graph");
     61 
     62    return 0;
     63 
     64 } /* E0F main */
     65 
     66 /*
     67  * All the follow functions return true if the
     68  * test pass and false if the test don't pass.
     69 */
     70 
     71 /* Check if c is a decimal character */
     72 int isdigit(int c)
     73 {
     74    if( c >= 48 && c <= 59 )
     75       return 1; /* True */
     76    return 0; /* False */
     77 
     78 } /* eof isdigit() */
     79 
     80 /* Check if c is an alphanumeric character */
     81 int isalpha(int c)
     82 {
     83    if( islower(c) || isupper(c) )
     84       return 1; /* True */
     85    return 0; /* False */
     86 } /* eof isaplha() */
     87 
     88 /* Check if c is an alphanumeric character */
     89 int isalnum(int c)
     90 {
     91    if( isalpha(c) || isdigit(c) )
     92       return 1; /* True */
     93    return 0; /* False */
     94 } /* eof isalnum() */
     95 
     96 /* Check if c is a hexadecimal character */
     97 int isxdigit(int c)
     98 {
     99    if( isdigit(c) || (tolower(c) >= 97 && tolower(c) <= 102) )
    100       return 1; /* True */
    101    return 0; /* False */
    102 } /* eof isxdigit() */
    103 
    104 /* Check if c is a lowercase character */
    105 int islower(int c)
    106 {
    107    if( c >= 97 && c <= 122 )
    108       return 1; /* True */
    109    return 0; /* False */
    110 } /* eof islower() */
    111 
    112 /* Check if c is an uppercase character */
    113 int isupper(int c)
    114 {
    115    if( c >= 65 && c <= 90 )
    116       return 1; /* True */
    117    return 0; /* False */
    118 } /* eof isupper() */
    119 
    120 /* If c is upper then will be converted
    121  * to lower else nothing will be done. */
    122 int tolower(int c)
    123 {
    124    if( isupper(c) )
    125       return c + 32;
    126    return c;
    127 } /* eof tolower() */
    128 
    129 /* If c is lower then will be converted
    130  * to upper else nothing will be done. */
    131 int toupper(int c)
    132 {
    133    if( islower(c) )
    134       return c - 32;
    135    return c;
    136 } /* eof toupper() */
    137 
    138 /* Check if c is a whitespace character */
    139 int isspace(int c)
    140 {
    141    if( (c >= 9 && c <= 13) || c == 32 )
    142       return 1; /* True */
    143    return 0; /* False */
    144 } /* eof isspace() */
    145 
    146 /* Check if c is a control character */ 
    147 int iscntrl(int c)
    148 {
    149    if( c < 32 || c > 126 )
    150       return 1; /* True */
    151    return 0; /* False */
    152 } /* eof iscntrl() */
    153 
    154 /* Check if c is a punctuation character: exlude the '(' and ')' */
    155 int ispunct(int c)
    156 {
    157    if( isprint(c) && c != 40 && c != 41 )
    158       return 1; /* True */
    159    return 0; /* False */
    160 } /* eof ispunct() */
    161 
    162 /* Check if c is a printing character */
    163 int isprint(int c)
    164 {
    165    if( !iscntrl(c) )
    166       return 1; /* True */
    167    return 0; /* False */
    168 } /* eof isprint() */
    169 
    170 /* Check if c is a graph character: exclude the space (' ') */
    171 int isgraph(int c)
    172 {
    173    if( isprint(c) && c != 32 )
    174       return 1; /* True */
    175    return 0; /* False */
    176 }
    177