forked from CPRF-Session2/Assignment8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcount.c
More file actions
95 lines (74 loc) · 1.88 KB
/
wordcount.c
File metadata and controls
95 lines (74 loc) · 1.88 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Jared Wasserman -- wordcount.c */
/* This program takes a text file and prints to the screen the number of words in the file. The program will stop reading when it find ######## anywhere in the text */
#include <stdio.h>
#include <string.h>
/*Checks for 8 consecutive # (indicating to end reading the file)*/
int checkForEnding(int c, char line[]){
int b;
int count=0;
for(b=c;b<(c+8);b++){
if(line[b]=='#'){
count++;
}else{
b=c+8;
}
}
if(count==8){
return 1;
}else{
return 0;
}
}
int main(){
int wordCount=0;
FILE *inFile = fopen("text.txt","r");
int i = 0;
char line[200];
fgets(line,200,inFile);
line[strlen(line)-1]='\0';
int c;
int keepReading=1;
/*Loop for every line of file*/
while(keepReading){
/*Iterates through the line*/
for(c=0;c<strlen(line);c++){
/*If it finds #. Checks for 8 and if so ends the reading.*/
if(line[c]=='#'){
if(checkForEnding(c,line)){
keepReading=0;
}
}
if(!keepReading){
break;
}
/*Checks for whitespace. Continues if it finds whitespace.*/
if(line[c]==' '){
continue;
}else{ /*If it doesnot find whitespace. Icrements the counter to where there is whitespace and also checks for ######## to end the program.*/
int a=c;
while(line[a]!=' '){
if(line[a]=='#'){
if(checkForEnding(a,line)){
keepReading=0;
break;
}else{
a++;
}
}else{
a++;
}
}
/*Adds to wordcount and then continues the next iteration of the forloop where at the end of the word.*/
c=a-1;
wordCount++;
continue;
}
}
fgets(line,sizeof(line),inFile);
line[strlen(line)-1]='\0';
i++;
}
printf("\nThere were %d words in this file\n\n",wordCount);
fclose(inFile);
return 0;
}