Cod sursa(job #1991125)

Utilizator arcoC. Nicolae arco Data 15 iunie 2017 12:28:18
Problema Text Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 0.86 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.in", "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 != ' ')
				{
					if(isalpha(current))
					{
						current_word++;
					}
				}
				else
				{
					words_number++;
					lungime_totala += current_word;
					current_word = 0;
				}
			}
		}

		fprintf(out, "%d\n", lungime_totala / words_number);

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

	return 0;
}