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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
#include <iostream>
using namespace std;
const int MAX_LEN = 1024;
//returns -1 if searched is not found in str. returns the index otherwise.
int contains(const char* str, const char* searched)
{
int len1 = strlen(str);
int len2 = strlen(searched);
for (int i = 0; i < len1 - len2 + 1; i++)
{
bool found = true;
for (int j = 0; j < len2; j++)
{
if (searched[j] != str[i + j])
{
found = false;
break;
}
}
if (found)
return i;
}
return -1;
}
bool containsOnlyWhiteSpaces(const char* str)
{
while (*str != '\0')
{
if (*str != ' ')
return false;
str++;
}
return true;
}
void makeSpaces(char* str, int startIndex, int spacesCount)
{
for (int i = startIndex; i < startIndex + spacesCount; i++)
str[i] = ' ';
}
bool hasSameWords(const char* sentence1, const char* sentence2)
{
int len1 = strlen(sentence1);
int len2 = strlen(sentence2);
char* s1 = new char[len1 + 1];
char* s2 = new char[len2 + 1];
char* s1Original = s1;// we change s1. So we could delete the whole array
strcpy(s1, sentence1);
strcpy(s2, sentence2);
for (int i = 0; i <= len1; i++)
{
if (s1[i] != ' ' &&s1[i] != '\0' )
continue;
//we are here if we found a space (or a \0 which means it's the last word).
//if the space is at the begining, there is not a word before it. So we just remove it.
if (i == 0)
{
s1 += 1;
len1--;
i = -1; // so it could become 0 for the next iteration
continue;
}
char* currentWord = new char[i + 1];
s1[i] = '\0';
strcpy(currentWord, s1);
if (i != len1) //if it's not the last word (because after the last word there is an '\0').
s1[i] = ' ';
int res = contains(s2, currentWord);
if (res == -1)
return false;
//we make the matched word as whitespaces.
makeSpaces(s1, 0, i);
makeSpaces(s2, res, i);
//we move the pointer to continue with the sentense.
s1 += i;
len1 -= i;
i = -1; // so it could become 0 for the next iteration
delete[] currentWord;
}
bool res = containsOnlyWhiteSpaces(s1Original) && containsOnlyWhiteSpaces(s2);
delete[] s1Original, s2;
return res;
}
int main()
{
char sentence1[MAX_LEN];
char sentence2[MAX_LEN];
//cin.getline(sentence1, MAX_LEN);
//cin.getline(sentence2, MAX_LEN);
cout << hasSameWords("hello friends bye enemy", "bye friends hello enemy");
}
|