回复:(aipb2007)贴代码
#include<iostream >
using spacename std;
#include<string >
void main()
{
string str ;
cout << "please type in the string" << endl ;
cin >> str ;
// define three counter for number, letter(upper,lower), other characters , separately.
int num = 0 ;
int letter = 0 ;
int otherChar = 0 ;
for(string::iterator iter = str.begin() ; iter != str.end() ; ++iter)
{
if ( (*iter == '0')
||(*iter == '1')
||(*iter == '2')
||(*iter == '3')
||(*iter == '4')
||(*iter == '5')
||(*iter == '6')
||(*iter == '7')
||(*iter == '8')
||(*iter == '9')
)
{
++num;
}
else if( (*iter == 'a')
||(*iter == 'A')
||(*iter == 'b')
||(*iter == 'B')
||(*iter == 'c')
||(*iter == 'C')
||(*iter == 'd')
||(*iter == 'D')
||(*iter == 'e')
||(*iter == 'E')
||(*iter == 'f')
||(*iter == 'F')
||(*iter == 'g')
||(*iter == 'G')
||(*iter == 'h')
||(*iter == 'H')
||(*iter == 'i')
||(*iter == 'I')
||(*iter == 'j')
||(*iter == 'J')
||(*iter == 'k')
||(*iter == 'K')
||(*iter == 'l')
||(*iter == 'L')
||(*iter == 'm')
||(*iter == 'M')
||(*iter == 'n')
||(*iter == 'N')
||(*iter == 'o')
||(*iter == 'O')
||(*iter == 'p')
||(*iter == 'P')
||(*iter == 'q')
||(*iter == 'Q')
||(*iter == 'r')
||(*iter == 'R')
||(*iter == 's')
||(*iter == 'S')
||(*iter == 't')
||(*iter == 'T')
||(*iter == 'u')
||(*iter == 'U')
||(*iter == 'v')
||(*iter == 'V')
||(*iter == 'w')
||(*iter == 'W')
||(*iter == 'x')
||(*iter == 'X')
||(*iter == 'y')
||(*iter == 'Y')
||(*iter == 'z')
||(*iter == 'Z'))
{
++letter ;
}
else
{
++otherChar ;
}
}
cout << "there is " << num << " numbers, " << letter << " letters, " << otherChar
<< " other characters. " << endl ;
}