Cod sursa(job #2453324)

Utilizator cristi1990anCornea Cristian cristi1990an Data 3 septembrie 2019 14:12:20
Problema Text Scor 40
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int is_letter(unsigned char A)
{
	if ((A >= 'a' && A <= 'z') || (A >= 'A' && A <= 'Z'))
		return 1;
	return 0;
}



int main()
{
	FILE* f = fopen("text.in", "rt");
	unsigned char text[1000000];
	unsigned long count = 0, len = 0, sum = 0, i = 0, v[1000];

	for (int j = 0; j < 1000; j++)
		v[j] = 0;

	fgets(text, 1000000, f);

	//printf("%s \n", text);

	do
	{
		if (is_letter(text[i]))
		{
			printf("Letter %c : \n", text[i]);
			if (len == 0)
				count++;
			len++;
		}
		else
			if (len != 0)
			{
				v[len]++;
				printf("Found a word of lenght %u. There are also %u other words of the same lenght found so far. \n", len, v[len] - 1);
				len = 0;
			}
		i++;

	} while (text[i] != '\0');

	v[len]++;

	for (int j = 0; j < 1000; j++)
		if (v[j] != 0)
		{
			printf("j=%d, v[%d]=%u \n", j, j, v[j]);
			sum = sum + (v[j] * j);
		}

	//printf("Total number of words: %u \n", count);

	//printf("sum=%u count=%u sum/count=%u \n", sum, count, (sum / count));
	sum = sum / count;

	f = fopen("text.out", "wt");
	fprintf(f, "%lu \n", sum);
	fflush(stdout);
	fclose(f);
	return 0;
}