-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount101.java
More file actions
29 lines (29 loc) · 1.45 KB
/
Copy pathcount101.java
File metadata and controls
29 lines (29 loc) · 1.45 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
import java.io.FileReader;
import java.io.IOException;
class count101 {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("input.txt");
int expected[] = {49,48,49}; // A.S.C.I.I Values of Each Character In The String to be Searched.
int size = expected.length; // Length of the String to be Searched.
int counter=0; // Counter : To count no. of Occurrences in the File.
int tally=0; // Tally : To Keep Track of Progress of Matching.
int i;
while((i=reader.read())!=-1) { // Traversing Through A File, Character By Character
if(i==expected[tally]) {
tally++; // Update the Tracker
if(tally==size) { // To Check the Tracker doesn't exceed the original length of the String To Be Searched.
counter++; // Updating the Occurrence Counter
tally=1; // As the Matched String conatins the Index 0 of the Expected String.
}
}
else {
if(i==49) // To Compensate for the Breakdown on Equal Start & End Character of the Expected String.
tally=1;
else
tally=0; // Break the Match Streak
}
}
reader.close(); // Closing the reader Object
System.out.println("No. of 101's in the Given File is: "+counter); //Print Statement
}
}