Skip to content

Commit 989ae65

Browse files
committed
Add function to convert time to words representation
Implemented the 'timeInWords' function to return the time in natural language format (e.g., "quarter past three"). This includes handling cases for "past" and "to" minutes as well as edge cases like "o' clock" and "half past". Added supporting ltrim and rtrim functions for string trimming.
1 parent 627ff72 commit 989ae65

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

TheTimeInWords.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
string ltrim(const string &);
6+
string rtrim(const string &);
7+
8+
/*
9+
* Complete the 'timeInWords' function below.
10+
*
11+
* The function is expected to return a STRING.
12+
* The function accepts following parameters:
13+
* 1. INTEGER h
14+
* 2. INTEGER m
15+
*/
16+
17+
string timeInWords(int h, int m) {
18+
// Words for numbers 1 to 29
19+
vector<string> words = {
20+
"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
21+
"eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", "seventeen",
22+
"eighteen", "nineteen", "twenty", "twenty one", "twenty two", "twenty three",
23+
"twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine"
24+
};
25+
26+
if (m == 0) {
27+
// Exactly on the hour
28+
return words[h] + " o' clock";
29+
} else if (m <= 30) {
30+
// Minutes past the hour
31+
if (m == 15) {
32+
return "quarter past " + words[h];
33+
} else if (m == 30) {
34+
return "half past " + words[h];
35+
} else if (m == 1) {
36+
return "one minute past " + words[h];
37+
} else {
38+
return words[m] + " minutes past " + words[h];
39+
}
40+
} else {
41+
// Minutes to the next hour
42+
int minutes_to = 60 - m;
43+
int next_hour = (h % 12) + 1; // Handle 12-hour format
44+
45+
if (minutes_to == 15) {
46+
return "quarter to " + words[next_hour];
47+
} else if (minutes_to == 1) {
48+
return "one minute to " + words[next_hour];
49+
} else {
50+
return words[minutes_to] + " minutes to " + words[next_hour];
51+
}
52+
}
53+
}
54+
55+
int main()
56+
{
57+
ofstream fout(getenv("OUTPUT_PATH"));
58+
59+
string h_temp;
60+
getline(cin, h_temp);
61+
62+
int h = stoi(ltrim(rtrim(h_temp)));
63+
64+
string m_temp;
65+
getline(cin, m_temp);
66+
67+
int m = stoi(ltrim(rtrim(m_temp)));
68+
69+
string result = timeInWords(h, m);
70+
71+
fout << result << "\n";
72+
73+
fout.close();
74+
75+
return 0;
76+
}
77+
78+
string ltrim(const string &str) {
79+
string s(str);
80+
81+
s.erase(
82+
s.begin(),
83+
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
84+
);
85+
86+
return s;
87+
}
88+
89+
string rtrim(const string &str) {
90+
string s(str);
91+
92+
s.erase(
93+
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
94+
s.end()
95+
);
96+
97+
return s;
98+
}

0 commit comments

Comments
 (0)