A solution to Problem A (5 point)

There are many ways of solve problem A. This program demonstrates several things. This was a question from the N.Z. 1995 programming competition. There were teams (from Auckland :-) that could not do this in five hours.

This is how I would do it. If you know a better way, let me know, that's how we all learn.

Good things
  • The use of assert(). This is the greatest command you can ever use. Use it lots. It will become your friend. :-)
    [True! Assert is an ideal way of catching design or programming errors. Normally one should NOT use assert to catch RUNTIME errors, so things like files not opening properly should be handled by outputting a message, or by throwing an exception. But in this programming competition, where user-interface is irrelevant, using assert to do it is okay. Mark Utting]
  • I/O from a string instead of a file. This allows you to grab the entire line and then process the string as if you were reading from a normal file.
  • Not so good things
  • Compile time constants. We are told the limits for this problem, but what would you do if a line could be any length?
  • Why 60+1+1? C strings are delimited by a 0. But fgets() also grabs the line feed character (\n), So we need 60 characters for the line, plus one for the end-of-string specifier, plus one more for the \n.
  • Stripping off the '\n'. It looks horrible.
  • strtok() is very useful. Pity it has a horrid interface.

  • // A solution to problem A by Stuart Inglis, in C
    #include < string.h >
    #include < stdlib.h >
    #include < stdio.h >
    #include < assert.h >
    #define MAXLEN (60+1+1)
    int main(void)
      FILE *infile;
      infile=fopen("PROBLEMA.DAT", "rt");
        char linebuffer[MAXLEN],*p;
        int count,len;
        if(linebuffer[len-1]=='\n') linebuffer[len-1]='\0';
        if(strcmp(linebuffer,"#")==0) break;
        p=strtok(linebuffer," ");
          p=strtok(NULL," ");
        fprintf(stdout,"The line contains %d words\n",count);
      return 0;