Using a map is a very good approach for handling this kind of stuff. However, if you don't want to use STL, you can use an array.
/*---------------------------------------------------------------------------
File name: LetterStats.cpp
Author: HJin
Created on: 7/2/2007 20:36:21
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
Analysis:
---------------------------------------------------------------------------
Sample output:
---------------------------------------------------------------------------
A: 1
B: 1
C: 1
D: 0
E: 0
F: 0
G: 1
H: 2
I: 2
J: 0
K: 0
L: 2
M: 1
N: 4
O: 1
P: 1
Q: 0
R: 0
S: 2
T: 1
U: 0
V: 0
W: 2
X: 0
Y: 0
Z: 0
a: 8
b: 3
c: 8
d: 7
e: 18
f: 7
g: 3
h: 5
i: 16
j: 2
k: 0
l: 2
m: 6
n: 12
o: 18
p: 3
q: 0
r: 14
s: 11
t: 16
u: 7
v: 2
w: 1
x: 0
y: 4
z: 0
Press any key to continue . . .
Reference:
*/
#include <iostream>
#include <fstream>
using namespace std;
/** content of a.txt
WASHINGTON (CNN) -- President Bush commuted Monday the prison term of
former White House aide I. Lewis "Scooter" Libby, facing 30 months in
prison after a federal court convicted him of perjury, obstruction of
justice and lying to investigators.
*/
int main(int argc, char** argv)
{
/**
c[0..25] for letter A..Z
c[26..51] for letter a..z
*/
int c[52];
int i;
for(i=0; i<52; ++i)
c[i] = 0;
ifstream ifs("a.txt");
if(!ifs)
{
cout<<"cannot open file a.txt.\n";
exit(0);
}
char ch;
while(ifs.get(ch))
{
if(ch>='A' && ch <= 'Z')
++c[ch-'A'];
if(ch>='a' && ch <= 'z')
++c[ch-'a'+26];
}
ifs.close();
for(i=0; i<26; ++i)
{
cout<<char('A'+i)<<": "<<c[i]<<endl;
}
for(i=26; i<52; ++i)
{
cout<<char('a'+i-26)<<": "<<c[i]<<endl;
}
return 0;
}
I am working on a system which has no Chinese input. Please don\'t blame me for typing English.