[prev] 57 [next]

Profile Example

Consider the following program that
  • searches for words in text containing a given substring
  • displays each such word once (in alphabetical order)

int main(int argc, char*argv[])
{
    char  word[MAXWORD];  // current word
    List  matches;        // list of matched words
    char  *substring;     // string to look for
    FILE  *input;         // the input file

    /* ... Check command-line args, open input file ... */

    /* Process the file - find the matching words */
    matches = NULL;
    while (getWord(input, word) != NULL) {
        if (contains(word,substring)
                 && !member(matches,word))
            matches = insert(matches,word);
    }
    printWords(matches);
    return 0;
}