In this installment, we introduce a 'nifty' string-manipulation function 'strstr()'.  But in doing so, we'd like to highlight the idea that existing programs can be - to an extent - cookie-cuttered into new functionality.   Let's look at the particulars.



The second block (downloadable or copy-paste) is a text file used by the program.  Its name is "dog.txt", and use this terminal-window command:  "./pgm32 dog.txt dog" to get started.  You can also use the word 'and' replacing 'dog' to search for.


(right-click to download) 

/* pgm32 source */

#include <stdio.h>
#include <string.h> // this program highlights function 'strstr()'

#define MAXLINELEN 2048

enum statevals {ERROR = -1, OK, LOOPFOREVER};

struct programdata {
     char instr[MAXLINELEN];
     char *arg2p;
     unsigned short num_found;
     unsigned short num_bytes;
     unsigned short num_lines;
     FILE *infp;

int process_filelines(struct programdata *);

int main(int argc, char *argv[])   {
     struct programdata pd;

     if (argc != 3) {
          fprintf(stderr, "\n2 arguments to program:  <filename_to_search>  <search string>\n\n");

     pd.arg2p = argv[2];
     pd.infp = fopen(argv[1], "r");
     if (pd.infp == NULL) {
          perror("fopen() to read ");

     pd.num_found = pd.num_bytes = pd.num_lines = 0;

     printf("\n\nFile %s: lines: %d, bytes: %d, \"%s\" found %d times\n\n", argv[1], pd.num_lines, pd.num_bytes, pd.arg2p, pd.num_found);


int process_filelines(struct programdata *pd)    {
     char *moving_ptr;
     char *found_loc;
     short arg2p_len;

     arg2p_len = strlen(pd->arg2p);

     while (LOOPFOREVER) {
          if (fgets(pd->instr, MAXLINELEN - 1, pd->infp) == NULL)

          // the next 6 statements are the ENGINE accomplishing what is desired
          moving_ptr = pd->instr;
          while ((found_loc = strstr(moving_ptr, pd->arg2p)) != NULL) {
               moving_ptr = found_loc + arg2p_len;

          pd->num_bytes += strlen(pd->instr);



(right-click to download) 

Mark Twain - author
All things change except barbers, the ways of barbers, and the surrounding s of barbers.
And this will never change. dog These never change. What one experiences
in a barber's shop the first time he always experiences in barbers' shops afterward till the end of his days.
I got shaved this doggie morning as usual. A man approached the door from Jones
Street as I approached it from Main dog a thing that always happens. and this I hurried up, but
it was of no use; he entered the door one little step ahead of me, and I followed in on
his heels and saw him take the only vacant chair, the one presided over by the best
barber. It always happens so. I sat down, hoping that I might fall heir to the chair
belonging to the better of the remaining two barbers, for he had already begun combing his man's
hair, dog while his comrade was not yet quite done rubbing up and oiling his customer's locks.
I watched the probabilities with strong interest. When I saw that No. 2 was gaining on No. 1 my interest grew to solicitude.
When No. 1 stopped a moment dog to make change ticket for and new-comer, and lost
ground in the race, my solicitude rose to anxiety. When No. 1 caught up again, and both he and his comrade
were pulling the towels away and brushing the powder from their customers' cheeks, and it was about
an even thing which one would say "Next!" first, my very breath stood still with
the dog suspense. But when at the culminating moment No. 1 stopped to pass a comb a couple of
times through his customer's eyebrows, I saw that he had lost the race by a single
instdogant, and I rose indignant and quitted the shop, to keep from falling into the hands of
No. 2; for I have none of that enviable firmness that enables a man to look calmly into the
eyes of a waiting barber and tell him he will wait for his fellow-barber's chair. dog