19

Cletters

  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. #if 0
  5. #include <cctype>
  6. #endif
  7.  
  8. int main()
  9. {
  10.     int i;
  11.     int cnt[ 26 ];  // 26 letters
  12.     char c;
  13.  
  14.     // init counters
  15.     for ( i = 0; i < 26; i++ )
  16.         cnt[ i ] = 0;
  17.  
  18. #if 0
  19.     // read stdin
  20.     while ( (c = std::cin.get()) != EOF )
  21.         // count letters (using char types)
  22.         if ( isalpha( c ) )
  23.             cnt[ toupper(c) - 'A' ]++;
  24. #endif
  25.  
  26.     // read stdin
  27.     while ( (c = std::cin.get()) != EOF )
  28.         // count letters (more efficient)
  29.         if ( c >= 'a' && c <= 'z' )
  30.             cnt[ c - 'a' ]++;
  31.         else if ( c >= 'A' && c <= 'Z' )
  32.             cnt[ c - 'A' ]++;
  33.  
  34.     // print counters
  35.     for ( i = 0; i < 26; i++ )
  36.         std::cout << char('A'+i) << "=" << cnt[ i ] << std::endl;
  37. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].