Conditional branching - which 'if ( )' and 'else ( )' afford us - has a step-brother called "the SWITCH / CASE' block.  Here we look at that interesting feature of 'C'.  We also touch on using 'man' pages as an immediate language / function reference.


  why not try using the MAN page to find out what 'strstr()' does, and how to use it. In the terminal window, enter "man strstr" ('q' exits MAN).  Also try a MAN query for 'strcat()' using "man strcat"...


(right-click to download)  

/* pgm15 source */

#include <stdio.h>
#include <string.h>    // we use 'strstr()' in this program

#define STRINGLEN 375

void load_up_string(char *);

int main(int argc, char *argv[]) {
     char line[STRINGLEN];

     memset(line, '\0', STRINGLEN);   // null-out the entire string
     load_up_string(&line[0]);               // now fill the string with words

     switch (argc) {
          case 1 : { // = NO command line parameters, just show string
               printf("\n%s\n\n", line);
               printf("The 'line[]' array is %ld characters large.\n\n", strlen(line));
               printf("Now, run program again with something to search for.\n");
          case 2 : { // = ONE command line parameter, search for it
               if (strstr(line, argv[1]) == NULL)
                    printf("\nAwww.... didn't find %s. No cookie for user. Try again.\n", argv[1]);
                   printf("\nBingo!! \"%s\" was found. We'll send you $3.00!\n", argv[1]);
          default : { // user gave > 1 command line parameter
               printf("\nPlease run this program with 0 or 1 command line items.\n");


void load_up_string(char *line) {
     strcat(line, "When in the course of human events, it becomes necessary ");
     strcat(line, "to decide upon a particular food or drink to consume in the ");
     strcat(line, "presence of others (either friend or foe), one must take into ");
     strcat(line, "account: Why, Where, When, Who, Which, and Monkey. ");
     strcat(line, "A doctor a day keeps the apple away. A nine in time saves ");
     strcat(line, "some number of stitches. You can tune a piano but you ");
     strcat(line, "can't tuna fish.");