-
Notifications
You must be signed in to change notification settings - Fork 0
/
10293 Word Length and Frequency.cpp
73 lines (61 loc) · 1.14 KB
/
10293 Word Length and Frequency.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
#include<stdio.h>
#include<assert.h>
#include<string.h>
char word[1000][100];
char s[1000];
#define MAX 81
#define WORDLEN 31
int freq[WORDLEN];
void process(char word[][100], int cnt){
int l;
int flag;
int prevLen = -1, curLen;
for(int i=0;i<WORDLEN; i++)
freq[i] = 0;
flag = false;
for(int i=0; i<cnt; i++){
int l = strlen(word[i]);
curLen = 0;
for(int j=0; word[i][j]; j++){
if(j == l-1 && word[i][j] == '-'){
flag = true;
prevLen = curLen;
curLen = 0;
}
else if(word[i][j] == ' ' || word[i][j] == '?' || word[i][j] == '!' || word[i][j] == ',' || word[i][j] == '.'){
if(flag){
freq[prevLen + curLen]++;
prevLen = -1;
flag = false;
}
else if(curLen)
freq[curLen]++;
curLen = 0;
}
else if(word[i][j] != '-' && word[i][j] != '\''){
curLen++;
}
}
if(curLen && !flag)
freq[curLen]++;
}
for(int i=1;i<WORDLEN; i++){
if(freq[i]){
printf("%d %d\n", i, freq[i]);
}
}
printf("\n");
}
int main(){
int flag;
int cnt = 0;
while(gets(s) && s[0]){
if(s[0] == '#'){
process(word, cnt);
cnt = 0;
continue;
}
strcpy(word[cnt++], s);
}
return 0;
}