-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcosequentialMatch.cpp
More file actions
73 lines (59 loc) · 1.47 KB
/
Copy pathcosequentialMatch.cpp
File metadata and controls
73 lines (59 loc) · 1.47 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
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
string firstFile = "FirstFile.txt";
string secondFile = "SecondFile.txt";
string outputFile = "OutputFile.txt";
void readNames(string filename) {
fstream fp;
fp.open(filename, ios::out);
int numNames;
cout << "Enter number of names to enter in file " << filename << ": ";
cin >> numNames;
char name[20];
cout << "Enter the names in ascending order: " << endl;
for (int i = 0; i < numNames; i++) {
cin >> name;
fp << name << endl;
}
fp.close();
}
int getNames(string filename, char list[][20]) {
int idx = 0;
fstream fp;
fp.open(filename, ios::in);
while (!fp.eof()) {
fp.getline(list[idx], 20);
idx++;
}
fp.close();
return idx-1;
}
int main() {
readNames(firstFile);
readNames(secondFile);
char firstList[100][20];
char secondList[100][20];
int n = getNames(firstFile, firstList);
int m = getNames(secondFile, secondList);
fstream fp;
fp.open(outputFile, ios::out);
int i = 0;
int j = 0;
cout << "Names common to both files are: " << endl;
while (i < n && j < m) {
int cmp = strcmp(firstList[i], secondList[j]);
if (cmp == 0) {
fp << firstList[i] << endl;
cout << firstList[i] << endl;
i++; j++;
} else if (cmp < 0) {
i++;
} else {
j++;
}
}
fp.close();
return 0;
}