Cod sursa(job #1493314)

Utilizator clau05Claudiu Avram clau05 Data 28 septembrie 2015 23:43:49
Problema Text Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <locale>
#include <cstring>
using namespace std;

#define BUFFER_SIZE 1024

int main() {
	char buffer[BUFFER_SIZE];
	int word_count = 0;
	int alpha_letter_count = 0;
	bool last_was_alpha = false;

	ifstream in("text.in");
	in.width(BUFFER_SIZE - 1);
	while (true) {
		in.read(buffer, sizeof(buffer) - 1);
		int read_count = in.gcount();
		if (in.eof()) {
			break;
		}
		buffer[read_count] = 0;
		for (int i = 0; i < strlen(buffer); i++) {
			bool now_is_alpha = isalpha(buffer[i]);
			if (now_is_alpha) {
				alpha_letter_count++;
			}
			if (!last_was_alpha && now_is_alpha) {
				word_count++;
			}
			last_was_alpha = now_is_alpha;
		}
	}

	ofstream out("text.out");
	out << (alpha_letter_count / word_count) << endl;
}