-
Notifications
You must be signed in to change notification settings - Fork 0
/
010_Replace Alphabet Position.cpp
268 lines (233 loc) · 6.16 KB
/
010_Replace Alphabet Position.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Codewars Coding Challenge
Replace With Alphabet Position.
In this kata you are required to, given a string, replace every letter with its position in the alphabet.
If anything in the text isn't a letter, ignore it and don't return it.
"a" = 1, "b" = 2, etc.
Example
alphabet_position("The sunset sets at twelve o' clock.")
Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" ( as a string )
https://www.codewars.com/kata/546f922b54af40e1e90001da/train/cpp
*/
// My Solution
#include <string>
std::string alphabet_position(const std::string &text){
std::string temp = ""; // buat variabel temp untuk menyimpan posisi alphabet
for(char c : text){ // lakukan iterasi ke setiap karakter pada text
if(isalpha(c)){ // periksa apakah menemukan karakter alphabet?
temp += std::to_string(tolower(c) - 'a' + 1) + " "; // kalau ada, konversi karakter c ke posisinya sesuai urutan alphabet
}
}
// hapus spasi di akhir temp bila ada
if(!temp.empty()){
temp.pop_back();
}
return temp;
}
// Sample test
/*
Describe(Sample_Tests) {
It(Tests) {
Assert::That(alphabet_position("The sunset sets at twelve o' clock."), Equals("20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11"));
Assert::That(alphabet_position("The narwhal bacons at midnight."), Equals("20 8 5 14 1 18 23 8 1 12 2 1 3 15 14 19 1 20 13 9 4 14 9 7 8 20"));
Assert::That(alphabet_position("0123456789"), Equals(""));
Assert::That(alphabet_position(",./<>?-_=+ "), Equals(""));
Assert::That(alphabet_position(""), Equals(""));
}
};
*/
/*
PERFECT SOLUTION FROM CODEWARS
=1=
#include <string>
std::string alphabet_position(const std::string &text) {
std::string str1;
int d=text.length();
char ch;
std::string word;
for(int i=0; i<d; i++) {
ch = text[i];
switch(ch) {
case 'a':
case 'A':
word = "1";
break;
case 'b':
case 'B':
word = "2";
break;
case 'c':
case 'C':
word = "3";
break;
case 'd':
case 'D':
word = "4";
break;
case 'e':
case 'E':
word = "5";
break;
case 'f':
case 'F':
word = "6";
break;
case 'g':
case 'G':
word = "7";
break;
case 'h':
case 'H':
word = "8";
break;
case 'i':
case 'I':
word = "9";
break;
case 'j':
case 'J':
word = "10";
break;
case 'k':
case 'K':
word = "11";
break;
case 'l':
case 'L':
word = "12";
break;
case 'm':
case 'M':
word = "13";
break;
case 'n':
case 'N':
word = "14";
break;
case 'o':
case 'O':
word = "15";
break;
case 'p':
case 'P':
word = "16";
break;
case 'q':
case 'Q':
word = "17";
break;
case 'r':
case 'R':
word = "18";
break;
case 's':
case 'S':
word = "19";
break;
case 't':
case 'T':
word = "20";
break;
case 'u':
case 'U':
word = "21";
break;
case 'v':
case 'V':
word = "22";
break;
case 'w':
case 'W':
word = "23";
break;
case 'x':
case 'X':
word = "24";
break;
case 'y':
case 'Y':
word = "25";
break;
case 'z':
case 'Z':
word = "26";
break;
}
if(!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
continue;
}
else
str1 += word + " ";
}
if(str1 != "")
str1.erase(str1.length()-1);
return str1;
}
=2=
#include <string>
using namespace std;
std::string alphabet_position(const std::string &text) {
string res = "";
for (char c : text) {
c = tolower(c);
if ('a' <= c && c <= 'z') {
if (res != "") {
res += " ";
}
res += to_string(c - 'a' + 1);
}
}
return res;
}
=3=
#include <string>
std::string alphabet_position(const std::string &text) {
std::string result = "";
for (size_t i = 0; i < text.size(); i++) {
char c = text[i];
if (std::isalpha(c)) {
if (!result.empty())
result += " ";
result += std::to_string(std::toupper(c) - 64);
}
}
return result;
}
=4=
#include <string>
#include <string.h>
std::string alphabet_position(const std::string& text) {
const char* c_str = text.c_str();
std::string result;
for (int i = 0; i < (int)strlen(c_str); i++) {
if (isalpha(c_str[i])) {
if (result != "")
result += " ";
result += std::to_string((int)tolower(c_str[i]) - 96);
}
}
return result;
}
=5=
#include <iostream>
#include <string>
using namespace std;
string alphabet_position(const string &text)
{
string sAlphabet = "abcdefghijklmnopqrstuvwxyz";
string bAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string result = "";
for (char c : text)
{
if ((c >= 'a' && c <= 'z'))
{
result += to_string(sAlphabet.find(c) + 1) + " ";
}
if ((c >= 'A' && c <= 'Z'))
{
result += to_string(bAlphabet.find(c) + 1) + " ";
}
}
return result.substr(0, result.size() - 1);
}
*/