-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings_Main.cpp
More file actions
169 lines (134 loc) · 3.63 KB
/
Strings_Main.cpp
File metadata and controls
169 lines (134 loc) · 3.63 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
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
// ===========================================================================
// Strings_Main.cpp
// ===========================================================================
// includes
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
// function prototypes
static bool isLeapYear(int year);
static int maxNumOfDays(int month, int year);
static bool IsValid(int day, int month, int year);
static bool verifyDateFormat(const std::string& date);
static std::string dateToWord(const std::string& date);
static void testVerifyDateFormat();
static void testDateToWord();
// implementation
static bool verifyDateFormat(const std::string& date)
{
// accepted format: "tt.mm.jjjj"
if (date.length() != 10) {
return false;
}
else if (date[2] != '.' || date[5] != '.') {
return false;
}
int indices[]{ 0, 1, 3, 4, 6, 7, 8, 9 };
for (int i = 0; i < 8; ++i) {
int index = indices[i];
if (std::isdigit(date[index]) == 0) {
return false;
}
}
return true;
}
static std::string dateToWord(const std::string& date)
{
static std::string months[12] =
{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
if (!verifyDateFormat(date)) {
return "";
}
// verify values
std::string sDay = date.substr(0, 2);
std::string sMonth = date.substr(3, 2);
std::string sYear = date.substr(6, 4);
// convert strings to ints
int day = std::stoi(sDay);
int month = std::stoi(sMonth);
int year = std::stoi(sYear);
if (! IsValid(day, month, year)) {
return "";
}
std::string label = months[month - 1];
// remove leading '0', if any
if (sDay[0] == '0') {
sDay = sDay[1];
}
std::string word = sDay + ". " + label + " " + sYear;
return word;
}
static void testVerifyDateFormat()
{
// test 'verifyDateFormat'
std::cout << std::boolalpha << verifyDateFormat("10.08.2000") << std::endl;
std::cout << std::boolalpha << verifyDateFormat("ABCDEFGHIJK") << std::endl;
std::cout << std::boolalpha << verifyDateFormat("10:08:4321") << std::endl;
std::cout << std::boolalpha << verifyDateFormat("10.XX.4321") << std::endl;
}
static void testDateToWord()
{
// test 'dateToWord'
std::string s = dateToWord("12.08.2000");
std::cout << s << std::endl;
s = dateToWord("01.13.2023");
std::cout << s << std::endl;
s = dateToWord("32.12.2023");
std::cout << s << std::endl;
s = dateToWord("01.01.2023");
std::cout << s << std::endl;
s = dateToWord("31.12.2023");
std::cout << s << std::endl;
}
static bool isLeapYear(int year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
static int maxNumOfDays(int month, int year)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return (isLeapYear(year)) ? 29 : 28;
default:
return 0;
}
}
static bool IsValid(int day, int month, int year)
{
if (year < 0) {
return false;
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > maxNumOfDays(month, year)) {
return false;
}
return true;
}
void exerciseStrings()
{
//testVerifyDateFormat();
testDateToWord();
}
// ===========================================================================
// End-of-File
// ===========================================================================