-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-CharactersClassification.cpp
76 lines (64 loc) · 2.11 KB
/
09-CharactersClassification.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This program get a string from user and
* count the number of characters which is
* found this string, in several categories
* and display the result.
*/
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
void counter(string userString)
{
int numAlNum = 0;
int numAlpha = 0;
int numBlank = 0;
int numCntrl = 0;
int numDigit = 0;
int numGraph = 0;
int numLower = 0;
int numPrint = 0;
int numPunct = 0;
int numSpace = 0;
int numUpper = 0;
int numXDigit = 0;
int totalNumChar = 0;
int i;
for(i = 0; userString[i] != '\0'; i++)
{
if ( isalnum(userString[i]) ) numAlNum++;
if ( isalpha(userString[i]) ) numAlpha++;
if ( isblank(userString[i]) ) numBlank++;
if ( iscntrl(userString[i]) ) numCntrl++;
if ( isdigit(userString[i]) ) numDigit++;
if ( isgraph(userString[i]) ) numGraph++;
if ( islower(userString[i]) ) numLower++;
if ( isprint(userString[i]) ) numPrint++;
if ( ispunct(userString[i]) ) numPunct++;
if ( isspace(userString[i]) ) numSpace++;
if ( isupper(userString[i]) ) numUpper++;
if ( isxdigit(userString[i]) ) numXDigit++;
}
totalNumChar = i;
cout << "\n In your string: "
<< "\n total character count : " << totalNumChar
<< "\n alphanumeric character count : " << numAlNum
<< "\n alphabetic character count : " << numAlpha
<< "\n blank character count : " << numBlank
<< "\n punctuation character count : " << numPunct
<< "\n decimal digit character count : " << numDigit
<< "\n hexadecimal digit character count : " << numXDigit
<< "\n lowercase letter character count : " << numLower
<< "\n uppercase letter character count : " << numUpper
<< "\n printable character count : " << numPrint
<< "\n control character count : " << numCntrl
<< "\n white-space character count : " << numSpace
<< "\n " << numGraph << " character has graphical representation in your string" << endl;
}
int main(int argc, char **argv)
{
string userString;
cout << "Please enter a string: \n";
getline(cin, userString);
counter(userString);
return 0;
}