aboutsummaryrefslogtreecommitdiff
path: root/tests/sysy_lvX/047_word_count.sy
blob: bf72c730bf9b1b95ebf92d446c5f13a51e2ce0ff (plain)
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
// Count all the word in the string

int wc(int string[], int n) {
    int inWord;
    int i;
    int count;
    i = 0;
    inWord = 0;
    count = 0;
    while (i < n) {
        if (string[i] != 32) {
            if (inWord == 0) {
                count = count + 1;
                inWord = 1;
            }
        } else {
            inWord = 0;
        }
        i = i + 1;
    }
    return count;
}

int main() {
    int string[500];
    int temp;
    int i;
    i = 0;
    temp = 0;
    while (temp != 10) {
        temp = getch();
        string[i] = temp;
        i = i + 1;
    }
    temp = wc(string, i);
    putint(temp);
    return 0;
}