Cod sursa(job #1558117)

Utilizator daniel.sanduSandu Daniel daniel.sandu Data 28 decembrie 2015 18:52:32
Problema Text Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <cstdio>
#include <string>

bool isLetter(char x) {
	return ('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z');
}

int wordDensity(const std::string& text) {
	bool isChain = false;
	int wordCount = 0, wordTotalLength = 0;
	for (auto it = text.cbegin(); it != text.cend(); ++it)
		if (isLetter(*it)) {
			isChain = true;
			++wordTotalLength;
		} else if (isChain) {
			isChain = false;
			++wordCount;
		}
  if (isChain)
		++wordCount;
	if (wordCount)
		return wordTotalLength / wordCount;
	return 0;
}

int main() {
	char const * const inputFile = "text.in",
						 * const outputFile = "text.out";
	std::ifstream in(inputFile);
	std::ofstream out(outputFile);

	std::string line, text;
	while (std::getline(in, line)) text += line;
	out << wordDensity(text);
	return 0;
}