Cod sursa(job #203013)

Utilizator szzeretlekxyz abcd szzeretlek Data 12 august 2008 17:01:37
Problema Text Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

enum state{
	IN_WORD,
	OUT_WORD
};

int main(){

	FILE* in, *out;
		
	
	enum state crt_state = OUT_WORD;
	char crt_symbol;
	

	int no_words = 0, no_letters = 0;
	in = fopen("text.in", "r");

	if(!in){
		fprintf(stderr,"Unable to open input file\n");
		return 1;
	}

	out = fopen("text.out", "w");

	if(!out){
		fprintf(stderr,"Unable to open output file\n");
		return 1;
	}

	while(1){
		crt_symbol = fgetc(in);
		if(EOF == crt_symbol)
			break;
		if(crt_state == OUT_WORD){
			if(isalpha(crt_symbol)){
				no_words++;
				no_letters++;
				crt_state = IN_WORD;
			}
		}
		else
		{
			if(isalpha(crt_symbol))
				no_letters++;
			else
				crt_state = OUT_WORD;
		}
	}

	if(0 == no_letters)
		fprintf(out,"0");
	else
		fprintf(out,"%d",no_letters/no_words);
	
	fclose(in);
	fclose(out);

	

	return 0;
}