Pagini recente » Cod sursa (job #1609053) | Cod sursa (job #3282793) | Cod sursa (job #1167995) | Cod sursa (job #1320629) | Cod sursa (job #203013)
Cod sursa(job #203013)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
enum state{
IN_WORD,
OUT_WORD
};
int main(){
FILE* in, *out;
enum state crt_state = OUT_WORD;
char crt_symbol;
int no_words = 0, no_letters = 0;
in = fopen("text.in", "r");
if(!in){
fprintf(stderr,"Unable to open input file\n");
return 1;
}
out = fopen("text.out", "w");
if(!out){
fprintf(stderr,"Unable to open output file\n");
return 1;
}
while(1){
crt_symbol = fgetc(in);
if(EOF == crt_symbol)
break;
if(crt_state == OUT_WORD){
if(isalpha(crt_symbol)){
no_words++;
no_letters++;
crt_state = IN_WORD;
}
}
else
{
if(isalpha(crt_symbol))
no_letters++;
else
crt_state = OUT_WORD;
}
}
if(0 == no_letters)
fprintf(out,"0");
else
fprintf(out,"%d",no_letters/no_words);
fclose(in);
fclose(out);
return 0;
}