Advertisement
sawyermade

hw7-no-bag

Jul 11th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. //string.h is used for puts, while stdlib is used for malloc/realloc
  2. //and ctype for isalnum and ispunct.
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7.  
  8. //function that checks if malloc and realloc succeed.
  9. void memCheck(char *str);
  10.  
  11. //main function.
  12. int main(int argc, char *argv[])
  13. {
  14.     //str is the string used to hold the contents of the file.
  15.     //ch temporarily holds the char being read from the file before it gets but in str.
  16.     //fp is the pointer to input stream from the file named in filename.
  17.     //x, y, z are counters. x for chars, y for words, and z for lines.
  18.     char *str, ch;
  19.     FILE *fp;
  20.     int x = 0, y = 0, z = 0;
  21.  
  22.     //tests for valid arguments.
  23.     if (argc-2)
  24.     {
  25.         puts("Invalid arguments. Usage: ./daniel_sawyer_hw07 NameOfFile.txt\nTry again.\n");
  26.         return 0;
  27.     }
  28.  
  29.     //it opens the file stream and checks to make sure the file is in the correct location.
  30.     fp = fopen(argv[1], "r");
  31.     if (fp == NULL)
  32.     {
  33.         puts("Invalid file. Usage: ./daniel_sawyer_hw07 NameOfFile.txt\nMake sure file is in same direcotry as binary and try again.\n");
  34.         return 0;
  35.     }
  36.  
  37.     //allocates memory for the str pointer effectively turning it into a char array size 1 and tests if its successful.
  38.     str = malloc(sizeof(char));
  39.     memCheck(str);
  40.  
  41.     //loop that reads every char in the input file and counts all the chars, words, and lines that are in the file.
  42.     while ((ch = getc(fp)) != EOF)
  43.     {
  44.         //counts words and lines. if previous character was alpha/num or punctuation, it increments the word variable y.
  45.         //also counts lines (z) everytime the \n char is detected.
  46.         switch(ch)
  47.         {
  48.             case ' '  : if ( isalnum(str[x-1]) || ispunct(str[x-1]) ) ++y; break;
  49.             case '\n' : if ( isalnum(str[x-1]) || ispunct(str[x-1]) ) ++y; ++z; break;
  50.         }
  51.  
  52.         //reallocates memory for the string to x+1 chars and then checks if allocation was successful.
  53.         str = realloc(str, (x+1) * sizeof(char));
  54.         memCheck(str);
  55.  
  56.         //puts the char in the string then increments x.
  57.         str[x++] = ch;
  58.     }
  59.    
  60.     //closes file stream since it is no longer needed.
  61.     fclose(fp);
  62.  
  63.     //increments z to the correct number of lines.
  64.     ++z;
  65.     //checks to make sure the last char was alpha/num or punctuation.
  66.     //if it is, it increments y (words) to the correct amount.
  67.     if ( isalnum(str[x-1]) || ispunct(str[x-1]) )
  68.         ++y;
  69.  
  70.     //prints the number of chars, words, and lines in the input file.
  71.     printf("\nNumber of Characters: %d\n", x);
  72.     printf("\nNumber of Words: %d\n", y);
  73.     printf("\nNumber of Lines: %d\n", z);
  74.  
  75.     //ends program.
  76.     return 0;
  77. }
  78.  
  79. //memory check function. if mem runs out, kills program.
  80. void memCheck(char *str)
  81. {
  82.     if (str == NULL)
  83.     {
  84.         puts("Memory allocation failed. Please try again.\n");
  85.         exit(EXIT_FAILURE);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement