Cod sursa(job #3279900)

Utilizator dariuseranDelca Darius dariuseran Data 24 februarie 2025 19:10:44
Problema Text Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 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> // Pentru isalpha()

using namespace std;

ifstream cin("text.in");
ofstream cout("text.out");

int main() {
    string text;
    getline(cin, text); // Citim toată linia de text

    int length_sum = 0, word_count = 0;
    int word_length = 0;

    for (char c : text) {
        if (isalpha(c)) {
            word_length++; // Contorizăm lungimea cuvântului curent
        } else if (word_length > 0) {
            length_sum += word_length;
            word_count++;
            word_length = 0; // Resetăm pentru următorul cuvânt
        }
    }

    // Dacă ultimul caracter a fost literă, trebuie să contorizăm ultimul cuvânt
    if (word_length > 0) {
        length_sum += word_length;
        word_count++;
    }

    // Evităm împărțirea la zero
    cout << (word_count > 0 ? length_sum / word_count : 0) << "\n";

    return 0;
}