-
Notifications
You must be signed in to change notification settings - Fork 0
/
findwords.cpp
110 lines (100 loc) · 2.57 KB
/
findwords.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
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>
#include <cstring>
#include <stdlib.h>
using namespace std;
const int cols = 16, rows = 15;
char words[rows][cols] = { "tgbwwinterwsesn",
"aaunttmmhfoodnb",
"jlwcqldzmpmvdmr",
"asagmquwvvbsohi",
"bwplotanadtpgnc",
"rewngodjcpnatnk",
"eeotwosbqharrsa",
"azcgeswewnaknpb",
"dinnerqodlwdcar",
"onopkwmparktzcc",
"qbfrogmamwpweey",
"lqzqnnmrzjjsclg",
"mosgzczetdbooto",
"pdcrzmsngrdnrpz",
"ohnkzwaterjgtra"};
char *getWordVertical(int columnIndex);
char *reverse(char *strToReverse);
bool searchVertical(char *strCase);
bool searchHorizontal(char *strCase);
int main(){
char word[16];
int n;
cin >> n;
cin.ignore(1, '\n');
for(int i = 0; i < n; i++){
cin.getline(word, 16, '\n');
if(searchVertical(word) || searchHorizontal(word)){
cout << "Ada\n";
}
else{
cout << "Tidak Ada\n";
}
}
return 0;
}
char *getWordVertical(int columnIndex){
char *verticalWord = (char *) malloc(16);
for(int i = 0; i < 15; i++){
verticalWord[i] = words[i][columnIndex];
}
return verticalWord;
}
char *reverse(char *strToReverse){
char *reverseResult = (char *) malloc(strlen(strToReverse));
for(unsigned int i = 0; i < strlen(strToReverse); i++){
reverseResult[i] = strToReverse[strlen(strToReverse) - 1 - i];
}
return reverseResult;
}
bool searchVertical(char *strCase){
unsigned int count = 0;
for (int i=0; i<15; i++){
char *holdwords =(char *)malloc(31);
strcpy(holdwords,getWordVertical(i));
strcat(holdwords,reverse(holdwords));
for(int j=0; j<strlen(holdwords); j++){
if(strCase[count]==holdwords[j]){
count++;
}
else if (holdwords[j]==strCase[0]){
count =1;
}
else{
count =0;
}
if(count ==strlen(strCase)){
return true;
}
}
}
return false;
}
bool searchHorizontal(char *strCase){
unsigned int count = 0;
for(int i = 0; i < 15; i++){
char *holdWords = (char *) malloc(31);
strcpy(holdWords, words[i]);
strcat(holdWords, reverse(holdWords));
for(unsigned int j = 0; j < strlen(holdWords); j++){
if(strCase[count] == holdWords[j]){
count++;
}
else if(holdWords[j] == strCase[0]){
count = 1;
}
else{
count = 0;
}
if(count == strlen(strCase)){
return true;
}
}
}
return false;
}