5
Cletters
- #include <iostream>
- #include <cstdio>
- #if 0
- #include <cctype>
- #endif
- int main()
- {
- int i;
- int cnt[ 26 ]; // 26 letters
- char c;
- // init counters
- for ( i = 0; i < 26; i++ )
- cnt[ i ] = 0;
- #if 0
- // read stdin
- while ( (c = std::cin.get()) != EOF )
- // count letters (using char types)
- if ( isalpha( c ) )
- cnt[ toupper(c) - 'A' ]++;
- #endif
- // read stdin
- while ( (c = std::cin.get()) != EOF )
- // count letters (more efficient)
- 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++ )
- std::cout << char('A'+i) << "=" << cnt[ i ] << std::endl;
- }
Commentaires