-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixme_hello.cpp
More file actions
66 lines (58 loc) · 1.38 KB
/
fixme_hello.cpp
File metadata and controls
66 lines (58 loc) · 1.38 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
6 kyu
FIXME: Hello
https://www.codewars.com/kata/5b0a80ce84a30f4762000069
*/
#include <regex>
#include <string>
class Dinglemouse {
std::string name;
int age;
char sex;
std::string msg;
public:
Dinglemouse();
Dinglemouse& setAge(int age);
Dinglemouse& setSex(char sex);
Dinglemouse& setName(const std::string& name);
std::string hello();
};
Dinglemouse::Dinglemouse() {
this->msg = "Hello.";
}
Dinglemouse& Dinglemouse::setAge(int age) {
this->age = age;
std::string s = " I am " + std::to_string(age) + ".";
std::regex re("( I am [0-9]+[.])");
if (std::regex_search(this->msg, re))
this->msg = std::regex_replace(this->msg, re, s);
else
this->msg += s;
return *this;
}
Dinglemouse& Dinglemouse::setSex(char sex) {
this->sex = sex;
std::string s = " I am ";
if (sex == 'F')
s += "fe";
s += "male.";
std::regex re("( I am [fe]*male[.])");
if (std::regex_search(this->msg, re))
this->msg = std::regex_replace(this->msg, re, s);
else
this->msg += s;
return *this;
}
Dinglemouse& Dinglemouse::setName(const std::string& name) {
this->name = name;
std::string s = " My name is " + name + ".";
std::regex re("( My name is [a-zA-Z]+[.])");
if (std::regex_search(this->msg, re))
this->msg = std::regex_replace(this->msg, re, s);
else
this->msg += s;
return *this;
}
std::string Dinglemouse::hello() {
return msg;
}