Cod sursa(job #1941967)

Utilizator cosminbxCosmin Banu cosminbx Data 27 martie 2017 18:35:29
Problema Text Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>

int main(void)
{
    int ret = 0;

    FILE *fin = NULL;
    FILE *fout = NULL;

    uint32_t l = 0;
    uint32_t n = 0;
    int c;
    bool in_word = false;

    fin = fopen("text.in", "r");
    if (fin == NULL) {
        ret = -1;
        goto exit;
    }

    while ((c = fgetc(fin)) != EOF) {
        bool is_alpha = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
        if (is_alpha) {
            l++;
            if (!in_word) {
                in_word = true;
                n++;
            }
        } else {
            in_word = false;
        }
    }

    fout = fopen("text.out", "w");
    if (fout == NULL) {
        ret = -1;
        goto exit;
    }

    fprintf(fout, "%" PRIu32, l / n);

exit:

    if (fin == NULL) {
        fclose(fin);
    }
    if (fout == NULL) {
        fclose(fout);
    }

    return ret;
}