#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
char alphabet[ 26 ] = "abcdefghijklmnopqrstuvwxyz" ;
int count[ 26 ] ;
pthread_mutex_t mutex;
char * sentence( char * s) {
char * p;
char * q;
char * arr;
int i;
p = s;
arr = q;
for ( i= 0 ; * p != '.' ; i++ ) {
* q = * p;
q++;
p++;
}
* q = '\0 ' ;
return arr;
}
char ** load_sentence( char * p, char ** q, int i) {
return q;
}
void * count_letter( void * s) {
pthread_mutex_lock( & mutex) ;
char * p = ( char * ) s;
int i;
for ( i= 0 ; i< 26 ; i++ ) {
if ( * p == alphabet[ i] ) {
count[ i] ++;
}
}
pthread_mutex_unlock( & mutex) ;
return NULL;
}
void frequency( char * str) {
char * s = str;
int i, j, l;
pthread_t tid[ l] ;
if ( pthread_mutex_init( & mutex, NULL) != 0 ) {
printf ( "Mutex init failed\n " ) ; }
for ( i= 0 ; i< l; i++ ) {
pthread_create( & tid[ i] , NULL, count_letter, ( void * ) s) ;
s++;
}
for ( j= 0 ; j< l; j++ ) {
pthread_join( tid[ j] , NULL) ;
}
}
int main( int argc, char * argv[ ] ) {
int fd;
char buff[ 100 ] ;
char ** text
= malloc ( 10 * sizeof ( char * ) ) ; char * start = buff;
int i = 0 ; //number of phrases!
char * p = NULL;
while ( * ( p = sentence( start) ) != '\0 ' ) {
text = load_sentence( p, text, i) ;
i++;
}
int j, k;
for ( k= 0 ; k< i; k++ ) {
frequency( text[ k] ) ;
}
for ( j= 0 ; j< 26 ; j++ ) {
printf ( "%c : %d times\n " , alphabet
[ j
] , count
[ j
] ) ; }
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx1bmlzdGQuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RyaW5nLmg+CiNpbmNsdWRlIDxwdGhyZWFkLmg+CgpjaGFyIGFscGhhYmV0WzI2XSA9ICJhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5eiI7CmludCBjb3VudFsyNl07CnB0aHJlYWRfbXV0ZXhfdCBtdXRleDsKCmNoYXIqIHNlbnRlbmNlKGNoYXIgKiBzKXsKICAgIGNoYXIqIHA7CiAgICBjaGFyKiBxOwogICAgY2hhciogYXJyOwogICAgaW50IGk7CiAgICBwID0gczsKICAgIHEgPSBtYWxsb2MoMTAwKTsKICAgIGFyciA9IHE7CiAgICBmb3IgKGk9MDsgKnAgIT0gJy4nOyBpKyspeyAKICAgICAgICAqcSA9ICpwOwogICAgICAgIHErKzsKICAgICAgICBwKys7IAogICAgfQogICAgKnEgPSAnXDAnOwogICAgcmV0dXJuIGFycjsKfQoKY2hhcioqIGxvYWRfc2VudGVuY2UoY2hhciogcCwgY2hhcioqIHEsIGludCBpKXsKICAgIHFbaV0gPSBtYWxsb2Moc3RybGVuKHApKzEpOwogICAgc3RyY3B5KHFbaV0sIHApOwogICAgcmV0dXJuIHE7Cn0KCnZvaWQqIGNvdW50X2xldHRlcih2b2lkICogcyl7CiAgICBwdGhyZWFkX211dGV4X2xvY2soJm11dGV4KTsKICAgIGNoYXIqIHAgPSAoY2hhciopIHM7CiAgICBpbnQgaTsKICAgIGZvciAoaT0wOyBpPDI2OyBpKyspewogICAgICAgIGlmICgqcCA9PSBhbHBoYWJldFtpXSl7CiAgICAgICAgICAgIGNvdW50W2ldKys7CiAgICAgICAgfQogICAgfQogICAgcHRocmVhZF9tdXRleF91bmxvY2soJm11dGV4KTsKICAgIHJldHVybiBOVUxMOwp9Cgp2b2lkIGZyZXF1ZW5jeShjaGFyKiBzdHIpewogICAgY2hhciogcyA9IHN0cjsKICAgIGludCBpLCBqLCBsOwogICAgbCA9IHN0cmxlbihzdHIpOwogICAgcHRocmVhZF90IHRpZFtsXTsKICAgIGlmIChwdGhyZWFkX211dGV4X2luaXQoJm11dGV4LCBOVUxMKSAhPSAwKXsKICAgICAgICBwcmludGYoIk11dGV4IGluaXQgZmFpbGVkXG4iKTsKICAgICAgICBleGl0KDEpOwogICAgfQogICAgZm9yIChpPTA7IGk8bDsgaSsrKXsKICAgICAgICBwdGhyZWFkX2NyZWF0ZSgmdGlkW2ldLCBOVUxMLCBjb3VudF9sZXR0ZXIsICh2b2lkKikgcyk7CiAgICAgICAgcysrOwogICAgfQogICAgZm9yIChqPTA7IGo8bDsgaisrKXsKICAgICAgICBwdGhyZWFkX2pvaW4odGlkW2pdLCBOVUxMKTsKICAgIH0KfQoKCmludCBtYWluKGludCBhcmdjLCBjaGFyKiBhcmd2W10pewoKCiAgICBpbnQgZmQ7CiAgICBjaGFyIGJ1ZmZbMTAwXTsKICAgIGNoYXIgKiogdGV4dCA9IG1hbGxvYygxMCpzaXplb2YoY2hhciopKTsKICAgIHNjYW5mKCIlcyAiLCBidWZmKTsKICAgIHByaW50ZigiJXNcbiIsIGJ1ZmYpOwogICAgY2hhciogc3RhcnQgPSBidWZmOwogICAgaW50IGkgPSAwOyAvL251bWJlciBvZiBwaHJhc2VzIQogICAgY2hhciogcCA9IE5VTEw7CgogICAgd2hpbGUgKCoocCA9IHNlbnRlbmNlKHN0YXJ0KSkgIT0gJ1wwJyl7CiAgICAgICAgdGV4dCA9IGxvYWRfc2VudGVuY2UocCwgdGV4dCwgaSk7CiAgICAgICAgc3RhcnQgKz0gc3RybGVuKHApKzE7CiAgICAgICAgaSsrOwogICB9CgogICBpbnQgaiwgazsKCiAgIGZvciAoaz0wOyBrPGk7IGsrKyl7CiAgICAgICAgZnJlcXVlbmN5KHRleHRba10pOwogICB9CgogICBmb3IgKGo9MDsgajwyNjsgaisrKXsKICAgICAgICBwcmludGYoIiVjIDogJWQgdGltZXNcbiIsIGFscGhhYmV0W2pdLCBjb3VudFtqXSk7CiAgIH0KfQ==