8
Compter les lettres dans un document
- #include <stdio.h>
- main( ) {
- int c;
- int i;
- int cnt[ 26 ]; /* 26 letters */
- /* init counters */
- for ( i = 0; i < 26; i++ )
- cnt[ i ] = 0;
- /* read stdin */
- while ( (c = getchar()) != EOF )
- if ( c >= 'a' && c <= 'z' )
- cnt[ c - 'a' ]++;
- else if ( c >= 'A' && c <= 'Z' )
- cnt[ c - 'A' ]++;
- /* print counters */
- for ( i = 0; i < 26; i++ )
- printf("%c\t%d\n", 'A'+i, cnt[ i ] );
- }
Commentaires