Cod sursa(job #462591)

Utilizator dbalutaDaniel Baluta dbaluta Data 11 iunie 2010 21:09:54
Problema Text Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.8 kb
/**
 * Text - infoarena Educational Archive 
 * Daniel Baluta, <[email protected]> 
 * Copyright (C), 2010
 */

#include <stdio.h>
#include <ctype.h>

int main(void) 
{
	char c;
	int avg;
	FILE *f_in, *f_out;
	int total_len = 0, word_no = 0, have_word = 0;
	
	f_in = fopen("text.in", "r");
	if (!f_in) {
		perror("fopen");
		return -1;
	}

	f_out = fopen("text.out", "w");
	if (!f_out) {
		perror("fopen");
		return -1;
	}

	while(1) {

		c = fgetc(f_in);
		if (c == EOF) {//end of file
			if (have_word) 
				word_no++;
			break;
		}
		if ( isalpha(c) ) {
			total_len++;
			have_word = 1;
		}
		else 
		{
			if (have_word) {
				word_no++;
				have_word = 0;
			}
		}
	}
	if (!word_no) 
		avg = 0;
	else
		avg = total_len/word_no;
	fprintf(f_out, "%d", avg);
	
	fclose(f_in);
	fclose(f_out);

	return 0;
}