Cod sursa(job #3316813)

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

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

int returnWordsNumber(string line, int &totalLetters) {
	int length = line.size();
	int hasWord = 0;
	int numbersOfWords = 0;
	for (int i = 0; i < length; ++i) {
		if (isLetterOrDigit(line[i]) && hasWord == 0) {
			hasWord = 1;
			++totalLetters;
		} else if (isLetterOrDigit(line[i]) && hasWord == 1) {
			++totalLetters;
		} else if (!isLetterOrDigit(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;
	getline(fin, line);
	fin.close();
	if (returnWordsNumber(line, totalLetters) > 0) {
        fout << totalLetters / returnWordsNumber(line, totalLetters);
	} else {
	    fout << 0;
	}
	fout.close();
	return 0;
}