-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord Counting.cpp
77 lines (65 loc) · 1.68 KB
/
Word Counting.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
77
/*
In the name of God
Seyed Ali Mirferdos
Advanced Programming course
Winter 1395
*/
#include<stdio.h>
#include<string.h>
int main(){
/* You are allowed to enter a string with at last 1000 chars.
For example you can enter a hundred 10-char word*/
char text[1000];
char words[100][10] , repwords[100][10];
int wordRepeat[100] = {0};
printf("Please Enter your text below:\n");
fgets (text, 1000, stdin);
// Splitting text to words
for(int i = 0; text[i] != '\0'; i++)
if(text[i] < 'A' || text[i] > 'z')
text[i] = ' ';
char *token;
int i = 0;
const char s[2] = " ";
token = strtok(text, s);
while( token != NULL ){
strcpy(words[i], token);
i++;
token = strtok(NULL, s);
}
// Finding word repeats
int wordcount = 0;
for(int j = 0; j < i; j++){
int k = 0;
for(k; k < wordcount ; k++){
if(strcmp(words[j] , repwords[k]) == 0){
break;
}
}
if(k == wordcount){
strcpy(repwords[k], words[j]);
wordcount++;
}
wordRepeat[k]++;
}
// Finding Max & Min repeats
int max = 0 , min = 0;
int maxpos = 0 , minpos = 0;
for(int j = 0; j < wordcount; j++){
if(wordRepeat[j] > max){
max = wordRepeat[j];
maxpos = j;
}
if(wordRepeat[j] < min){
min = wordRepeat[j];
minpos = j;
}
}
// Printing the results
printf("\n================\nResults:\n");
for(int j = 0; j < wordcount; j++)
printf("%s : %d times\n", repwords[j], wordRepeat[j]);
printf("Most Repeated Word is %s : %d times\n", repwords[maxpos], wordRepeat[maxpos]);
printf("Least Repeated Word is %s : %d times\n", repwords[minpos], wordRepeat[minpos]);
return 0;
}