Cod sursa(job #1693644)

Utilizator sdwolfSDWOLF sdwolf Data 23 aprilie 2016 16:49:48
Problema Text Scor 70
Compilator c Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <stdio.h>
#include <stdbool.h>

int main() {
  FILE *input, *output;
  char symbol;
  int words, letters, state;
  bool is_letter;

  input = fopen("text.in", "r");

  // State:
  // -> 0: reading words
  // -> 1: reading delimiters
  state   = 0;
  words   = 0;
  letters = 0;

  while (fscanf(input, "%c", &symbol) != EOF) {
    is_letter = (symbol >= 'A' && symbol <= 'Z') || (symbol >= 'a' && symbol <= 'z');

    if (is_letter) {
      letters += 1;
      if (state == 1) {
        state = 0;
        words += 1;
      }
    } else {
      if (state == 0) {
        state = 1;
      }
    }
  }

  fclose(input);

  int result = letters / words;

  output = fopen("text.out", "w");
  fprintf(output, "%d\n", result);
  fclose(output);

  return 0;
}