Cod sursa(job #1764894)

Utilizator arcoC. Nicolae arco Data 26 septembrie 2016 04:22:48
Problema Text Scor 30
Compilator c Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

unsigned int get_tok_size(char *tok);

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

	if(in != NULL && out != NULL)
	{
		char *array = malloc(sizeof(char) * (1024 + 1));
		if(array != NULL)
		{
			fgets(array, 1024, in);
			char *tok = strtok(array, " ");
			unsigned int counter = 0;
			unsigned int total_lenght = 0;
			while(tok != NULL)
			{
				size_t tok_size = strlen(tok);
				if(tok_size > 1)
				{
					total_lenght += get_tok_size(tok);
				}
				counter++;
				tok = strtok(NULL, " ");
			}
			fprintf(out, "%u\n", total_lenght / counter);

			free(array);
		}
		else
		{
			printf("CANT ALLOCATE MEMORY\n");
		}

		fclose(in);
		fclose(out);
	}
	else
	{
		printf("CANT OPEN FILE(S)\n");
	}

    return 0;
}

unsigned int get_tok_size(char *tok)
{	
	unsigned int n = 0;
	for(char *cur = tok; *cur != '\0'; cur++)
	{
		if(isalpha(*cur))
		{
			n++;
		}
	}
	return n;
}