-4

I have to write this program on C, if someone could explain what to do and how to do it I would be most appreciative?

Using sequence and selection, write a program which asks the user to input a single character. The program must then compute and output the type of character input based upon the following ASCII classification table:

ASCII Classification:        Low:    High: 
Non-Printable                0       31 
Space                        32      32 
Symbol                       33      47
Digit                        48      57 
Symbol                       58      64 
Uppercase                    65      90 
Symbol                       91      96 
Lowercase                    97      122 
Symbol                       123     126 
Non-Printable                127     127
Spikatrix
  • 20,225
  • 7
  • 37
  • 83

2 Answers2

0

You can simply get the input with scanf("%c", &c); and just use a bunch of ifs, one for each range, and if it's within that range (since checking the value of c as an integer will show you exactly the ASCII number for that character), just print saying so. Here's a very basic example:

#include <stdio.h>

int main(int argc, char **argv)
{
        char c;

        scanf("%c", &c);

        if (c >= 65 && c <= 90)
                printf("%c is uppercase\n", c);

        else if (c >= 97 && c <= 122)
                printf("%c is lowercase\n", c);

        /* 
         * else if (...)
         *         ... (add code for other cases here, i.e., symbol/space/digit) 
         */

        else
                printf("character is non-printable\n");

        return 0;
}

These functions could be of use to you as well.

RastaJedi
  • 641
  • 1
  • 6
  • 18
  • 1
    It it worth noting that if `scanf ("%c", &c)` is used in a loop, you will loop twice for each input (you haven't accounted for `'\n'`). Also, you should check the `scanf` return. e.g. `if (scanf ("%c%*c", &c) == 1) ...` – David C. Rankin May 01 '16 at 08:41
0

You are in an ideal situation when you can take requirements and put them almost directly into code.

typedef struct  {
    char* classification;
    char low;
    char high;
} Classification;

Classification classifications[] = {
    { "Non-Printable                ", 0       , 31  },
    { "Space                        ", 32      , 32  },
    { "Symbol                       ", 33      , 47  },
    { "Digit                        ", 48      , 57  },
    { "Symbol                       ", 58      , 64  },
    { "Uppercase                    ", 65      , 90  },
    { "Symbol                       ", 91      , 96  },
    { "Lowercase                    ", 97      , 122 },
    { "Symbol                       ", 123     , 126 },
    { "Non-Printable                ", 127     , 127 }    
};

int main(void) {
    char test = '~';

    int numberOfClassifications = 
        sizeof(classifications)/sizeof(classifications[0]);

    int i;
    for (i = 0; i < numberOfClassifications; i++)
    {
        Classification classification = classifications[i];
        if (test >= classification.low && test <= classification.high)
        {
            puts(classification.classification);
            return 0; // success
        }
    }
    fputs("Character is not in the classification table.", stderr);
    return 1; // error
}

The question of what "Using sequence and selection" means went unanswered so this technique might not suit you this time. But it is a good thing to learn.

I pasted the table and then used a column mode editor to make it valid C by inserting { ", ",, , and }, into all the columns at once.

Community
  • 1
  • 1
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72