c-文字列「アルファベット、スペース、数字、その他の文字」の個数と行数を統計する.
7355 ワード
1 #include <stdio.h>
2 #include <ctype.h>
3
4 using namespace std;
5
6 /*
7 : , 、 、 。
8 */
9
10 void
11 count() {
12 // .
13 int letters = 0;
14 int spaces = 0;
15 int digit = 0;
16 int others = 0;
17 char curChar;
18 // , ( ) ,'
' ASCII 10, 0, '
' ( ).
19 while((curChar = getchar()) != '
') {
20 if(isalpha(curChar)) // curChar , c “isupper(curChar)||islower(curChar)”
21 ++letters;
22 else if(isdigit(curChar)) // curChar 0 9.
23 ++digit;
24 else if(isspace(curChar))
25 ++spaces;
26 else ++others;
27 }
28
29 printf("letters:%d, digits:%d, spaces:%d,others:%d
", letters, digit, spaces, others);
30 //cout<<"letters:"<<letters<<",digits:"<<digit<<",spaces:"<<spaces<<",others:"<<others<<endl;
31 }
32
33 // .
34 int
35 countLines(char *input) {
36 int lns = 0;
37 while(gets(input))
38 ++lns;
39 return lns;
40 }
41
42 int
43 main(void) {
44 printf("enter a string:");
45 count();
46
47 //char *t;
48 //gets(t);
49 //Run-Time Check Failure #3 - The variable 't' is being used without being initialized.
50 /*
51 , gets 't', . ,t ( ), gets .
52 */
53
54 char cs[10240];
55 int lns = countLines(cs);
56 printf("lines:%d
", lns);
57 }