Advertisement
dmilicev

character_analysis_in_file_v1.c

Feb 14th, 2020
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.74 KB | None | 0 0
  1. /*
  2.  
  3.     character_analysis_in_file_v1.c         by Dragan Milicev
  4.  
  5. Character analysis in a file.
  6.  
  7.  
  8. Ascii table:
  9.  
  10. characters_0    0 -  31   special characters
  11.  
  12. characters_1   32 -  47
  13.  
  14. 0 - 9    48 -  57   digits
  15.  
  16. characters_2   58 -  64
  17.  
  18. A - Z    65 -  90   capital letters
  19.  
  20. characters_3   91 -  96
  21.  
  22. a - z    97 - 122   small letters
  23.  
  24. characters_4  123 - 126
  25.  
  26.  
  27. Character occurrences statistics in a file.
  28. Total number of lines.
  29. Total number of words.
  30. The number of occurrences of each character.
  31.  
  32.  
  33. You can find all my C programs at Dragan Milicev's pastebin:
  34.  
  35. https://pastebin.com/u/dmilicev
  36.  
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. #define BUFFER_SIZE 100
  44. #define NUM_OF_ASCII_CHARACTERS 256
  45.  
  46.  
  47. int main(int argc, char *argv[]) {
  48.  
  49.     FILE* pointer_to_the_input_file;
  50.     FILE* pointer_to_the_output_file;
  51.     char input_file_name[BUFFER_SIZE];
  52.     char output_file_name[BUFFER_SIZE];
  53.     char ch;
  54.     int num_of_characters = 0;      // total number of characters in the input file
  55.     int num_of_lines = 0;           // total number of lines (rows) in the input file
  56.     int num_of_small_letters = 0;   // total number of small letters in the input file
  57.     int num_of_capital_letters = 0; // total number of capital letters in the input file
  58.     int num_of_digits = 0;          // total number of digits in the input file
  59.     int num_of_spaces = 0;          // total number of spaces (blanko) in the input file
  60.     int num_of_tabs = 0;            // total number of tabs in the input file
  61.     int num_of_spaces_and_tabs = 0; // total number of spaces and tabs in the input file
  62.     int arr_for_ch_occurrences[NUM_OF_ASCII_CHARACTERS]; // array for each character's occurrence numbers
  63.     int i;
  64.  
  65.     for(i=0;i<argc;i++)
  66.         printf("\n %3d. argument of program is \n %s \n", i, argv[i] );
  67.  
  68.     // get input file name
  69.     if( argc == 1 ) {   // if no arguments are given to the program
  70.         printf("\n No arguments were provided when calling the program.\n");
  71.         printf("\n You can specify the name of the input file as the program argument.");
  72.         //exit(EXIT_FAILURE);
  73.  
  74.         // Enter the name of the input file in the program
  75.         printf("\n\n Enter name of the input file: ");
  76.         //scanf("%[^\n]",input_file_name);  // or, better, to awoid buffer overflow:
  77.         fgets(input_file_name,BUFFER_SIZE,stdin);
  78.         // We have to finish the string. Replace the newline with null char.
  79.         input_file_name[strlen(input_file_name)-1] = '\0';
  80.         //printf("\n|%s|\n",input_file_name);
  81.     }
  82.     else {              // arguments are given to the program
  83.         strcpy(input_file_name,argv[1]);
  84.     }
  85.  
  86.     //strcpy(input_file_name,"character_analysis_in_file_v1.c");
  87.  
  88.     // make output file name
  89.     strcpy(output_file_name,"statistic_for_");
  90.     strcat(output_file_name,input_file_name);
  91.     strcat(output_file_name,".txt");
  92.  
  93.     printf("\n Input file name:  %s \n",input_file_name);
  94.     printf("\n Output file name: %s \n",output_file_name);
  95.  
  96.     // open the input file
  97.     if( (pointer_to_the_input_file = fopen(input_file_name, "r")) == NULL ) {
  98.         fprintf(stderr, "\n\n Error opening input file %s ! \n\n", input_file_name );
  99.         exit(EXIT_FAILURE);
  100.     }
  101.  
  102.     // open the output file
  103.     if( (pointer_to_the_output_file = fopen(output_file_name, "w")) == NULL ) {
  104.         fprintf(stderr, "\n\n Error opening output file %s ! \n\n", output_file_name );
  105.         exit(EXIT_FAILURE);
  106.     }
  107.  
  108.     // Zeroing counter array
  109.     for(i=0;i<NUM_OF_ASCII_CHARACTERS;i++)
  110.         arr_for_ch_occurrences[i] = 0;      // array for each character's occurrence numbers
  111.  
  112.     // analysis of the input file,
  113.     // loading one character at a time from the input file to the end (EOF) of the input file
  114.     while( (ch = fgetc(pointer_to_the_input_file)) != EOF ) {
  115.  
  116.         num_of_characters++;            // Total number of characters
  117.  
  118.         arr_for_ch_occurrences[ch]++;   // array for each character's occurrence numbers
  119.  
  120.     } // while( (ch = fgetc(pointer_to_the_input_file)) != EOF )
  121.  
  122.  
  123.     // the number of lines (rows) is the same as the number of /n characters whose dec code is 10
  124.     num_of_lines = arr_for_ch_occurrences[10];
  125.  
  126.     for(i='0';i<='9';i++)               // total number of digits in the input file
  127.         num_of_digits = num_of_digits + arr_for_ch_occurrences[i];
  128.  
  129.     for(i='a';i<='z';i++)               // total number of small letters in the input file
  130.         num_of_small_letters = num_of_small_letters + arr_for_ch_occurrences[i];
  131.  
  132.  
  133.     for(i='A';i<='Z';i++)               // total number of capital letters in the input file
  134.         num_of_capital_letters = num_of_capital_letters + arr_for_ch_occurrences[i];
  135.  
  136.     // the spaces (blanks) characters number is the same as the number of character whose dec code is 32
  137.     num_of_spaces = arr_for_ch_occurrences[32];
  138.  
  139.     // the number of tabs is the same as the number of character whose dec code is 9
  140.     num_of_tabs = arr_for_ch_occurrences[9];
  141.  
  142.     // the number of tabs and spaces (blanks)
  143.     num_of_spaces_and_tabs = num_of_spaces + num_of_tabs;
  144.  
  145.  
  146.     // display the analysis results on screen and in output file
  147.     // (only shows characters that appear in the file)
  148.  
  149.     printf("\n\n In input file \"%s\" there are: \n\n", input_file_name );
  150.     fprintf(pointer_to_the_output_file,"\n\n In input file \"%s\" there are: \n\n", input_file_name );
  151.  
  152.     printf(" %7d characters \n\n", num_of_characters ); //  total number of characters in the input file
  153.     fprintf(pointer_to_the_output_file," %7d characters \n\n", num_of_characters );
  154.  
  155.     printf(" %7d lines (rows) \n\n", num_of_lines );     //  total number of lines (rows) in the input file
  156.     fprintf(pointer_to_the_output_file," %7d redova \n\n", num_of_lines );
  157.  
  158.     printf(" %7d small letters \n\n", num_of_small_letters );
  159.     fprintf(pointer_to_the_output_file," %7d small letters \n\n", num_of_small_letters );
  160.  
  161.     printf(" %7d capital letters \n\n", num_of_capital_letters );
  162.     fprintf(pointer_to_the_output_file," %7d capital letters \n\n", num_of_capital_letters );
  163.  
  164.     printf(" %7d digits \n\n", num_of_digits );
  165.     fprintf(pointer_to_the_output_file," %7d digits \n\n", num_of_digits );
  166.  
  167.     printf(" %7d spaces \n\n", num_of_spaces );
  168.     fprintf(pointer_to_the_output_file," %7d spaces \n\n", num_of_spaces );
  169.  
  170.     printf(" %7d tabs \n\n", num_of_tabs );
  171.     fprintf(pointer_to_the_output_file," %7d tabs \n\n", num_of_tabs );
  172.  
  173.     printf(" %7d spaces and tabs \n\n", num_of_spaces_and_tabs );
  174.     fprintf(pointer_to_the_output_file," %7d spaces and tabs \n\n", num_of_spaces_and_tabs );
  175.  
  176.     printf("\n\n hex dec   ch   br_ch \n\n\n");    // table header
  177.     fprintf(pointer_to_the_output_file,"\n\n hex dec   ch   br_ch \n\n\n");
  178.  
  179.     // display of visible characters
  180.     for(i=32;i<NUM_OF_ASCII_CHARACTERS;i++) {
  181.         if( arr_for_ch_occurrences[i] ) { // if the number of occurrences of the i-th character is greater than zero
  182.             printf("  %x %3d    %c %7d \n\n", i, i, i, arr_for_ch_occurrences[i] );
  183.             fprintf(pointer_to_the_output_file,"  %x %3d    %c %7d \n\n", i, i, i, arr_for_ch_occurrences[i] );
  184.         }
  185.     }
  186.  
  187.     // closing the input file
  188.     if( ( fclose(pointer_to_the_input_file) ) == EOF ) {    // close the input file
  189.         fprintf(stderr, "\n\n Error closing input file %s ! \n\n", input_file_name );
  190.         exit(EXIT_FAILURE);
  191.     }
  192.     else
  193.     printf("\n Input file \n %s \n successfully closed. \n", input_file_name );
  194.  
  195.     // closing the output file
  196.     if( ( fclose(pointer_to_the_output_file) ) == EOF ) {   // close the output file
  197.         fprintf(stderr, "\n\n Error closing output file %s ! \n\n", output_file_name );
  198.         exit(EXIT_FAILURE);
  199.     }
  200.     else
  201.     printf("\n Output file \n %s \n successfully closed. \n\n", output_file_name );
  202.  
  203.  
  204.     system("PAUSE");
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement