Cod sursa(job #3279903)

Utilizator dariuseranDelca Darius dariuseran Data 24 februarie 2025 19:13:24
Problema Text Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
/*#include <fstream>
#include <cstring>
using namespace std;
ifstream cin("text.in");
ofstream cout("text.out");
int main()
{
    int length_sum=0;
    int cnt=1;
    int length;
    bool ok=true;
    char text[10000];
    cin.get(text,10000);
    char* word;
    word= strtok(text," ,!:;.?");
    length_sum+=strlen(word);
    while(word!=NULL){
        length=strlen(word);
        for(int i=0;i<=strlen(word);i++)
        if(!isalnum(word[i]))
        length--;
        length_sum+=length;
        cnt++;
        word=strtok(NULL," ");
    }
    cout<<length_sum/cnt;

    return 0;
}*/
#include <fstream>
#include <cctype>

using namespace std;

ifstream fin("text.in");
ofstream fout("text.out");

int main() {
    char c;
    int length_sum = 0, word_count = 0, word_length = 0;

    while (fin.get(c)) {  // Citim caracter cu caracter
        if (isalpha(c)) {
            word_length++;  // Creștem dimensiunea cuvântului
        } else if (word_length > 0) {
            length_sum += word_length;
            word_count++;
            word_length = 0;  // Resetăm pentru următorul cuvânt
        }
    }

    // Dacă ultimul cuvânt nu s-a adăugat (nu s-a terminat cu separator)
    if (word_length > 0) {
        length_sum += word_length;
        word_count++;
    }

    fout << (word_count > 0 ? length_sum / word_count : 0) << "\n";

    return 0;
}