Pagini recente » Borderou de evaluare (job #2082783) | Borderou de evaluare (job #1608508) | Cod sursa (job #1970477) | Borderou de evaluare (job #2678049) | Cod sursa (job #3279903)
/*#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;
}