Cod sursa(job #807084)

Utilizator theep0Cruceru Radu theep0 Data 4 noiembrie 2012 05:51:44
Problema Text Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

bool is_letter(char c) {
    c = (int)c;
    return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
}

int main () {
    char letter;
    FILE *input_file;
    input_file = fopen("text.in", "r");
    bool counted_extra = true;
    int word_count = 0;
    int total_length = 0;
    int word_length;
    int i, l;

    while (!feof(input_file)) {
        fscanf(input_file, "%c", &letter);
        if (is_letter(letter)) {
            ++word_length;
        } else {
            if (word_length) {
                ++word_count;
                total_length += word_length;
            }
            word_length = 0;
        }
    }
    fclose(input_file);

    ofstream output_file;
    output_file.open("text.out");
    if (word_count == 0) {
        output_file << 0;
    } else {
        output_file << total_length / word_count;
    }
    output_file.close();
}