Cod sursa(job #385345)

Utilizator alexandru92alexandru alexandru92 Data 22 ianuarie 2010 17:20:44
Problema Text Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <cstring>
#include <cstdio>

/*
 *
 */
using namespace std;
int main()
{char *s;
    FILE *fin=fopen( "text.in", "rt" );
    fseek( fin, 0, SEEK_END ); //go to the end
    unsigned int size=ftell( fin ), nr=0, length=0, n, i, j; //get the size of the file
    rewind( fin );   //go back to the first line
    n=sizeof(char)*size;
    s=new char[ n ]; //alloc size
    fread( s, sizeof(char), size, fin ); //copy the data from the buffer to s
    for( i=0, n=strlen(s); i < n; i=j )
    {j=i+1;
       if( ( s[i] >= 'a' && s[i] <= 'z' ) || ( s[i] >= 'A' && s[i] <= 'Z' ) )
       {++nr; 
           for( ; j < n && ( ( s[j] >= 'a' && s[j] <= 'z' ) || ( s[j] >= 'A' && s[j] <= 'Z' ) ); ++j );
           length+=j-i;
       }
    }   
    if( nr )
        fprintf( fopen("text.out", "wt"), "%u", length/nr ); 
    return 0;
}