Pagini recente » Cod sursa (job #3267878) | Cod sursa (job #708322) | Borderou de evaluare (job #2319326) | Istoria paginii runda/racovita_ziua_nationala_11_12 | Cod sursa (job #807086)
Cod sursa(job #807086)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool is_letter(char c) {
c = (int)c;
return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
}
int main () {
char letter = '0';
FILE *input_file;
input_file = fopen("text.in", "r");
int word_count = 0;
int total_length = 0;
int word_length = 0;
while (letter != EOF) {
letter = fgetc(input_file);
if (is_letter(letter)) {
++word_length;
} else {
if (word_length) {
++word_count;
total_length += word_length;
word_length = 0;
}
}
}
fclose(input_file);
ofstream output_file;
output_file.open("text.out");
if (word_count == 0) {
output_file << 0;
} else {
output_file << total_length / word_count;
}
output_file.close();
}