lilboibickdick

Untitled

Jun 16th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. int fgetc(FILE *fp);
  4.  
  5. FILE *fp;
  6. int nlen;
  7. int nwords=0;
  8. int apos=0;
  9. int hist[12];
  10. char c;
  11. int getWordlen(void) {
  12.         //öffnen im Lesemodus
  13.         fp = fopen("darkweb2017-top1000.txt", "r");
  14.  
  15.         if(fp == NULL) {
  16.                 printf("Datei konnte nicht geöffnet werden.\n");
  17.         }else{
  18.  
  19.                 while((c = fgetc(fp)) != EOF) {
  20.  
  21.                         int result = isspace(c);
  22.                         if(result == 0) {
  23.                                 apos++;
  24.                         }else {
  25.                                 if(apos < 12) {
  26.                                         hist[apos] = hist[apos] + 1;
  27.                                         apos = 0;
  28.                                 }else {
  29.                                         hist[0] = hist[0] + 1;
  30.                                 }
  31.                                 nwords++;
  32.                         }
  33.  
  34.                 }
  35.  
  36.         }
  37. }
  38. void print_stars(int nlen, int nwords) {
  39.         float percent = (float)nlen/nwords*100;
  40.         printf("(%5.2f%%)  ", percent);
  41.         for(int i = 0; i <= percent; i++) {
  42.                 printf("*");
  43.         }
  44.         printf("\n");
  45. }
  46.  
  47. void print_histo(int histo[]) {
  48.         for(int i = 1; i < 12; i++) {
  49.                 printf("%i  -           %i:  ", i, histo[i]);
  50.                 print_stars(histo[i], nwords);
  51.         }
  52.         printf("There are %i words of length >=12.\n", histo[0]);
  53.         printf("In total ther are %i words.\n", nwords);
  54. }
  55.  
  56. int main() {
  57.         getWordlen;
  58.         print_histo(hist);
  59. }
Add Comment
Please, Sign In to add comment