Pagini recente » Cod sursa (job #366563) | Cod sursa (job #1425412) | Cod sursa (job #1050692) | Cod sursa (job #3142617) | Cod sursa (job #462590)
Cod sursa(job #462590)
/**
* Text - infoarena Educational Archive
* Daniel Baluta, <[email protected]>
* Copyright (C), 2010
*/
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char c;
int avg;
FILE *f_in, *f_out;
int total_len = 0, word_no = 0, have_word = 0;
f_in = fopen("text.in", "r");
if (!f_in) {
perror("fopen");
return -1;
}
f_out = fopen("text.out", "w");
if (!f_out) {
perror("fopen");
return -1;
}
while(1) {
c = fgetc(f_in);
if (c == EOF) //end of file
break;
if ( isalpha(c) ) {
total_len++;
have_word = 1;
}
else
{
if (have_word) {
word_no++;
have_word = 0;
}
}
}
if (!word_no)
avg = 0;
else
avg = total_len/word_no;
printf("%d\n", avg);
fprintf(f_out, "%d", avg);
return 0;
}