Cod sursa(job #1693649)

Utilizator sdwolfSDWOLF sdwolf Data 23 aprilie 2016 17:01:52
Problema Text Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.78 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:
  // -> -1: initial
  // -> 0:  reading letters
  // -> 1:  reading delimiters
  state   = -1;
  words   = 0;
  letters = 0;

  while (!feof(input)) {
    fscanf(input, "%c", &symbol);

    is_letter = (symbol >= 'A' && symbol <= 'Z') || (symbol >= 'a' && symbol <= 'z');

    if (is_letter) {
      letters += 1;

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

  fclose(input);

  int result = letters / words;

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

  return 0;
}