Cod sursa(job #3317680)

Utilizator stilus.office@gmail.comConstantin Diaconita [email protected] Data 24 octombrie 2025 21:35:59
Problema Text Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

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

int returnWordsNumber(string line, int &totalLetters) {
	int length = line.size();
	int hasWord = 0;
	int numbersOfWords = 0;
	for (int i = 0; i < length; ++i) {
		if (isLetter(line[i]) && hasWord == 0) {
			hasWord = 1;
			++totalLetters;
		} else if (isLetter(line[i]) && hasWord == 1) {
			++totalLetters;
		} else if (!isLetter(line[i]) && hasWord == 1) {
			++numbersOfWords;
			hasWord = 0;
		}
	}
	if (hasWord == 1) {
		++numbersOfWords;
	}
	return numbersOfWords;
}

int main() {
    ifstream fin("text.in");
    ofstream fout("text.out");
	string line;
	int totalLetters = 0;
	int totalWordsNumber = 0;
	while (!fin.eof()) {
		getline(fin, line);
		totalWordsNumber += returnWordsNumber(line, totalLetters);
	}
	if (totalWordsNumber != 0) {
		fout << totalLetters / totalWordsNumber;
	} else {
		fout << 0;
	}
	fout.close();
	return 0;
}