The following code was taken from an apparently working commercial product. Note that I've seen C code just as bad (if not worse!).

  CONST BLANK = ' ';
  TYPE CHAR80 = PACKED ARRAY [1..80] OF CHAR;

  FUNCTION trim (C : CHAR80) : CHAR80;

  VAR
    I        : INT;
    IN_QUOTE : BOOLEAN;
    J,K      : INT;
    T        : CHAR80;

  BEGIN
    I := 1;
    WHILE (I<80) AND (C[I] = BLANK) DO
      I := I + 1;
    IN_QUOTE := FALSE;
    FOR J := I TO 80 DO
      BEGIN
        IF (C[J] = #39) THEN
           IF (J=1) OR ((C[J+1]=BLANK) AND (C[J+2]=BLANK)) THEN
           IN_QUOTE := NOT IN_QUOTE;
        T[J-(I-1)] := C[J];
      END;
    W := 80;
    WHILE (W > 0) AND (T[W]=BLANK) DO
      W := W-1;
    trim  := T;
  END;

I claim that there are at least 6 bugs in the above code. (And that's not counting most of the readability problems.)

Try to identify the problems yourself, then click here to see my answers.

(Click here for the "How To Code Pascal" paper.)


(Updated 2000-05-03)