Week 4 Laboratory — Sample Solutions

  1. match.c

    // match.c: reads a (sub)string on the command line, and searches
    //          for this (sub)string in every string on stdin
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {
       int retval = EXIT_SUCCESS;
       if (argc == 2) {
          char word[BUFSIZ]; // BUFSIZ is large and is defined in stdio.h
          int found = 0;
          while (scanf("%s", word) != EOF) {      // see comment below
             if (strstr(word, argv[1]) != NULL) { // NULL means no match
                printf("%s\n", word);
                found = 1;
             }
          }
          if (!found) {
             printf("No match found\n");
          }
       } else {
          fprintf(stderr, "Usage: %s string\n", argv[0]);
          retval = EXIT_FAILURE;
       }
       return retval;
    }
    
    // undefined behaviour if length of word exceeds BUFSIZ
    // it would be better to use fgets(word, BUFSIZE, stdin)
    

  2. asci.c

    // asci.c: print the ASCII codes of printable, uppercase, lowercase and digits
    //         "Usage: %s [-printable,-upper,-lower,-digit]\n"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define PRINTABLE	"-printable"
    #define UPPER		"-upper"
    #define LOWER		"-lower"
    #define DIGIT		"-digit"
    #define LASTASCII	127
    #define FORMAT		8   // insert a \n every FORMAT chars
    
    int main(int argc, char *argv[]) {
       int someError = 1;
       int version = 0;
       int retval;
       if (argc == 1 || strcmp(argv[1], PRINTABLE) == 0) {
          version = 1;
       } else if (strcmp(argv[1], UPPER) == 0) {
          version = 2;
       } else if (strcmp(argv[1], LOWER) == 0) {
          version = 3;
       } else if (strcmp(argv[1], DIGIT) == 0) {
          version = 4;
       }
       if (argc <= 2) {
          int i;
          int count=1;
          for (i=0; i<=LASTASCII; i++) { // we try every code
             if ((version == 1 && isprint(i)) ||
                 (version == 2 && isupper(i)) ||
                 (version == 3 && islower(i)) ||
                 (version == 4 && isdigit(i))) {
                   printf("%d %c\t", i, i);
                   if (count % FORMAT == 0) {
                      putchar('\n');
                   }
                   someError = 0;
                   count++;
             }
          }
       }
       if (someError) {
          fprintf(stderr, "Usage: %s [-printable,-upper,-lower,-digit]\n", argv[0]);
          retval = EXIT_FAILURE;
       } else {
          putchar('\n');
          retval = EXIT_SUCCESS;
       }
       return retval;
    }
    

  3. matrix.c

    //matrix.c: a matrix implementation
    #include <stdio.h>
    #include "matrix.h"
    
    // read matrix values from stdin into "m"
    // return 1 if successful, else 0
    int readMatrix(Matrix m) {
       int i, j;
       int dataGood = 1;
       for (i=0; i<SIZE && dataGood; i++) {
          for (j=0; j<SIZE && dataGood; j++) {
             if (scanf("%d",&m[i][j]) != 1) {
                dataGood = 0;
             }
          }
       }
       return dataGood;
    }
    
    // copy values from "from" to "to"
    void copyMatrix(Matrix from, Matrix to) {
       int i, j;
       for (i=0; i<SIZE; i++) {
          for (j=0; j<SIZE; j++) {
             to[i][j] = from[i][j];
          }
       }
    }
    
    // compute sum of "a" + "b"; store result in "c"
    void addMatrix(Matrix a, Matrix b, Matrix c) {
       int i, j;
       for (i=0; i<SIZE; i++) {
          for (j=0; j<SIZE; j++) {
             c[i][j] = a[i][j] + b[i][j];
          }
       }
    }
    
    // are 2 matrices equal?
    // return 1 if equal, else 0
    int equalMatrix(Matrix a, Matrix b) {
       int i, j;
       int equal = 1; // assume equal
       for (i=0; i<SIZE && equal; i++) {
          for (j=0; j<SIZE && equal; j++) {
             if (a[i][j] != b[i][j]) {
                equal = 0;
             }
          }
       }
       return equal;
    }