Pagini recente » Cod sursa (job #2165524) | Cod sursa (job #1370018) | Cod sursa (job #1406782) | Cod sursa (job #580197) | Cod sursa (job #1764894)
#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;
}