Cod sursa(job #1991129)

Utilizator arcoC. Nicolae arco Data 15 iunie 2017 12:33:19
Problema Text Scor 60
Compilator c Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(void)
{
	FILE *in = fopen("text.in", "r");
	FILE *out = fopen("text.out", "w");

	if(in != NULL && out != NULL)
	{
		int words_number = 0;
		int lungime_totala = 0;

		int current_word = 0;
		while(1)
		{
			char current = getc(in);
			if(current == EOF)
			{
				if(current_word)
				{
					words_number++;
					lungime_totala += current_word;
				}
				break;
			}
			else
			{
				if(current != ' ' && !ispunct(current))
				{
					if(isalpha(current))
					{
						current_word++;
					}
				}
				else
				{
					if(current_word)
					{
						words_number++;
						lungime_totala += current_word;
						current_word = 0;
					}
				}
			}
		}

		// printf("words: %d\n", words_number);
		// printf("words len: %d\n", lungime_totala);
		fprintf(out, "%d\n", lungime_totala / words_number);

		fclose(in);
		fclose(out);
	}
	else
	{
		printf("Can't open file(s)\n");
	}

	return 0;
}