Cod sursa(job #824683)

Utilizator negrinegrean mihai negri Data 26 noiembrie 2012 20:50:33
Problema Text Scor 40
Compilator c Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <stdio.h>
#include <stdlib.h>

int main(){

	FILE *fp, *fr;
	char buf[1024];
	size_t nread;
	int i, spaces = 0, media, total = 0, totalSpaces = 0, removedElement = 0, totalWords = 0;
	
	fp = fopen("text.in","r");
	fr = fopen("text.out", "w");

	if(fp == NULL || fr == NULL){
		printf("could not open one of the files!");
		exit(0);
	}

	while ((nread = fread(buf, 1, sizeof buf, fp)) > 0){
        fwrite(buf, 1, nread, stdout);
		total = total + nread;
	}
	
	for(i = 0; i < total; i++){
		if(buf[i] == 32){
			spaces++;
		}	
		
		if((buf[i] < 48 || (buf[i] > 57 && buf[i] < 65) || (buf[i] > 90 && buf[i] < 97)  || buf[i] > 122) && buf[i] != 32){
			removedElement++;
		}
	}

	totalSpaces = spaces + 1;
	totalWords = (total - spaces) - removedElement;
	media = totalWords / totalSpaces; 

	fprintf(fr, "%d", media);

	fclose(fp);
	fclose(fr);
	

return 0;
}