/** * encode codeword - Encode standard input using the given codeword. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For getchar and printf #include // For strlen // +-----------+------------------------------------------------------ // | Constants | // +-----------+ /** * The minimum and maximum printable characters. */ #define MIN_CHAR 32 #define MAX_CHAR 127 /** * The default codeword (in case one does not get specified on the * command line. */ #define DEFAULT_CODEWORD "CSC161-01" // +------+----------------------------------------------------------- // | Main | // +------+ int main (int argc, char *argv[]) { char *codeword; // The codeword we're using int ch; // The input character int k; // The character in the codeword int e; // The encoded character int i=0; // The index in the codeword int len; // The length of the codeword int printable; // The number of printable characters // Set up information on the codeword if (argc < 2) codeword = DEFAULT_CODEWORD; else codeword = argv[1]; len = strlen (codeword); // Determine the number of printable characters printable = 1 + MAX_CHAR - MIN_CHAR; // Process the input while ((ch = getchar ()) != EOF) { // Echo non-printable characters verbatim if ((ch < MIN_CHAR) || (ch > MAX_CHAR)) printf ("%c", ch); else { // Get the next character in the code word. k = codeword[i]; i = (i + 1) % len; // printf ("ch=%d(%c), k=%d(%c), ", ch, ch, k, k); // TESTING // Convert each to a reasonable range ch -= MIN_CHAR; k -= MIN_CHAR; // Add them together e = (ch + k) % printable; // Return to a reasonable range e += MIN_CHAR; // And print it out // printf ("e=%d(%c)\n", e, e); // TESTING printf ("%c", e); } } // while // And we're done return 0; } // main