Cod sursa(job #807085)

Utilizator theep0Cruceru Radu theep0 Data 4 noiembrie 2012 06:03:57
Problema Text Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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

int main () {
    char letter = '0';
    FILE *input_file;
    input_file = fopen("text.in", "r");
    int word_count = 0;
    int total_length = 0;
    int word_length;

    while (letter != EOF) {
        letter = fgetc(input_file);
        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();
}