The Time in Words

Given the time in numerals we may convert it into words, as shown below:

At , use o' clock. For , use past

and for  use to. Note the space between the

 apostrophe and clock in o' clock. Write a program that

 prints the time in words for the input given in the format described.

Example:

Input:  h = 5, m = 47
Output: thirteen minutes to six

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string timeInWords(int hint m)
{

    vector<stringv = {"zero""one""two""three",
                        "four""five""six""seven""eight",
                        "nine""ten""eleven""twelve",
                        "thirteen""fourteen""fifteen",
                        "sixteen""seventeen""eighteen",
                        "nineteen""twenty""twenty one",
                        "twenty two""twenty three""twenty four",
                        "twenty five""twenty six""twenty seven",
                        "twenty eight""twenty nine"};

    string time;

    if (m <= 30)
    {
        if (m == 0)
            time = v[h] + " o' clock";
        else if (m == 15)
            time = "quarter past " + v[h];
        else if (m == 30)
            time = "half past " + v[h];
        else if (m == 1)
            time = v[m] + " minute past " + v[h];
        else
            time = v[m] + " minutes past " + v[h];
    }
    else
    {
        if (m == 45)
            time = "quarter to " + v[h + 1];
        else if (m == 59)
            time = v[60 - m] + " minute to " + v[h + 1];
        else
            time = v[60 - m] + " minutes to " + v[h + 1];
    }
    return time;
}

int main()
{

    int h = 5;
    int m = 47;

    cout << timeInWords(hm<< "\n";

    return 0;
}


No comments:

Post a Comment