-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.cpp
83 lines (78 loc) · 1.74 KB
/
day4.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
78
79
80
81
82
83
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAXWORDS = 20;
const int MAXWORDLEN = 20;
const int MAXLINELEN = 1000;
const char FILENAME[] = "input.txt";
bool isAnagram(const char* word1, const char* word2)
{
char w1[MAXWORDLEN], w2[MAXWORDLEN];
strcpy(w1, word1);
strcpy(w2, word2);
int len = strlen(w1);
if (len != strlen(w2))
return false;
bool match = false;
for (int i = 0; i < len; i++)
{
match = false;
for (int j = 0; j < len; j++)
{
if (w1[i] == w2[j])
{
match = true;
w2[j] = '\0';
break;
}
}
if (!match)
break;
}
return match;
}
bool isValidPhrase(char* phrase)
{
bool validPhrase = true;
char words[MAXWORDS][MAXWORDLEN];
int nWords = 0;
char* word = strtok(phrase, " ");
while (word)
{
for (int i = 0; i < nWords; i++)
if (strcmp(word, words[i]) == 0 || isAnagram(word, words[i]))
{
validPhrase = false;
break;
}
if (validPhrase)
{
strcpy(words[nWords], word);
nWords++;
}
else
break;
word = strtok(nullptr, " ");
}
return validPhrase;
}
int main()
{
ifstream stream;
stream.open(FILENAME);
if (stream.fail())
{
cout << "Couldn't open file" << endl;
exit(1);
}
char l[MAXLINELEN];
int nValidLines = 0;
while (stream.getline(l, MAXLINELEN))
if (isValidPhrase(l))
nValidLines++;
stream.close();
cout << nValidLines << endl;
}