The block of text below the source code should be saved to a file named 'text.txt' in the same location where the program will reside.

 

(right-click to download)

/* CExplorer - passing a pointer to structure. This code */
/* also introduces 'ctype.h' functions that assess bytes */
/* to determine what their attribute is. We also read */
/* lines from a pre-defined text file, and derive byte */
/* character statistics */

#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* must be present in same location as program */
#define DATAFILE "./text.txt"
#define STRINGLEN 128

/* there can be more categories */
/* but these are enough for now */
struct byte_categories {
     int file_size;
     int num_lines;
     int ch_islower;
     int ch_isupper;
     int ch_isdigit;
     int ch_isspace;
     int ch_ispunct;
     };

void parse_input_line(char *, struct byte_categories *);

int main(void) {
     char                             input_line[STRINGLEN];
     struct byte_categories bcats;
     FILE                            *fileptr;

     fileptr = fopen(DATAFILE, "r"); // "r" = read file
     if (fileptr == NULL) {
          perror("fopen() to read ");
          return(-1);
          }

     bcats.num_lines = 0; bcats.ch_islower = 0; bcats.ch_isupper = 0;
     bcats.ch_isdigit = 0; bcats.ch_isspace = 0; bcats.ch_ispunct = 0;
     bcats.file_size = 0;

     while (fgets(input_line, STRINGLEN - 1, fileptr) != NULL)
          parse_input_line(input_line, &bcats);

     fclose(fileptr); // we need to close what we opened
     printf("\n");

     printf("%s contains %d lines ", DATAFILE, bcats.num_lines);
     printf("and %d bytes.\n\n", bcats.file_size);
     printf("%5d lower-case letters\t", bcats.ch_islower);
     printf("%5d upper-case letters\n", bcats.ch_isupper);
     printf("%5d digits\t\t\t", bcats.ch_isdigit);
     printf("%5d whitespace\n", bcats.ch_isspace);
     printf("%5d punctuation\n\n", bcats.ch_ispunct);

     return(0);
     }

// we're returning the per-byte statistics through the
// "struct byte_categories" pointer *bcats
void parse_input_line(char *input_line, struct byte_categories *bcats) {
     int x;
     int str_len;

     str_len = strlen(input_line);
     bcats->file_size += str_len;
     bcats->num_lines++;

     for (x = 0; x < str_len; x++) {
          if (islower(*input_line) != 0) bcats->ch_islower++;
          if (isupper(*input_line) != 0) bcats->ch_isupper++;
          if (isdigit(*input_line) != 0) bcats->ch_isdigit++;
          if (isspace(*input_line) != 0) bcats->ch_isspace++;
          if (ispunct(*input_line) != 0) bcats->ch_ispunct++;

          input_line++; // sequentially access each element of the string
          }
     }

  

12 34 56 78 910 - is - a - series - of numbers
Here are ! so many @ non-alpha # characters %
on ^ these & lines ( ) { } to test things out.... ,,,,,
> ? / < and ++__ --
as if or and when to too two AAA BBB CCC

blank line blank line blank line
"What happens to a frog's car when it breaks down?"
"It gets toad away."

"What did the duck say when he bought lipstick?"
"Put it on my bill."
"Why did the can crusher quit his job?"
"Because it was soda pressing."
101010 !!! @@@ ~~~~ ````` hello
food drink hot cold KEWL =-+_=_+--+__
:: :: ;; ;;
"What do Japanese cows say to each other at the party?"
`````` "Hiyacowa"
"What do Japanese cows drink while at the party?"
"Cowasaki"
Here's a fine kettle of FISH
Apple Banana Carrot Danish Eggplant Fanta Guda Horsradish
Icicle Java Katsup Lemon Meat Nutrition Oreo Pizza Quiche
Rootbeer Salad Thyme Umon Vinagrette Watercres Xylophane
Yogurt Zabrinski