-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind4thstr.cpp
More file actions
45 lines (42 loc) · 1.41 KB
/
Find4thstr.cpp
File metadata and controls
45 lines (42 loc) · 1.41 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
using namespace std;
struct charnum {
char str[10];
int num;
};
int main()
{
FILE *fp = fopen("data.txt", "rw+");
char tmp[10] = {0};
struct charnum tmpstr;
map<int, struct charnum> mystr;
int num = 0;
int cou = 0;
bool found = false;
while (fscanf(fp, "%s", tmp) != EOF && !found) {
map<int, struct charnum>::iterator it;
for (it = mystr.begin(); it != mystr.end(); it++) {
if (strcmp(it->second.str, tmp) == 0) {
it->second.num++;
if (it->second.num == 2) {
cou++;
if (cou == 4) {
printf("The 4th str: %s", tmp);
found = true;
break;
}
}
}
}
if (it == mystr.end()) {
memcpy(tmpstr.str, tmp, sizeof(tmp));
tmpstr.num = 1;
printf("%s\n", tmp);
mystr[num++] = tmpstr;
}
}
fclose(fp);
}