Ask Question
19 October, 12:50

Write a C program that reads a time from the keyboard. The time should be in the format "HH:MM AM" or "HH:MM PM". Hours can be one or two digits, minutes are exactly two digits, and AM/PM can be in any mixture of uppercase and lowercase. For example, "1:10 am", "12:05 PM", and "12:45 aM" are valid inputs. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four-digit military time based on a 24-hour clock. For example, "1:10 AM" would output "0110 hours", "11:30 PM" would output "2330 hours", "12:15 AM" would output "0015 hours", and "5:05 Pm" would output "1705 hours". The function should return a string to be written to the screen by the main function

+1
Answers (2)
  1. 19 October, 14:09
    0
    Check the explanation

    Explanation:

    C+ + code:

    #include

    using namespace std;

    string convert (string s) {

    int l=s. length ();

    string ans=" hours";

    if (l==7) {

    if (s[5]=='a'||s[5]=='A') {

    ans[0]='0';

    ans[1]=s[0];

    ans[2]=s[2];

    ans[3]=s[3];

    else{

    if (s[6]=='a'||s[6]=='A') {

    ans[0]=s[0];

    ans[1]=s[1];

    ans[2]=s[3];

    ans[3]=s[4];

    return ans;

    }

    int main () {

    cout<<"Enter time: ";

    string s;

    getline (cin, s);

    cout<<"Corresponding military time is "<
    return 0;

    }
  2. 19 October, 14:09
    0
    See explaination

    Explanation:

    #include

    #include

    #include

    #include

    #include

    #include

    using namespace std;

    string itoa (int num)

    {

    stringstream converter;

    converter << num;

    return converter. str ();

    }

    string convert (string str)

    int main () {

    string ntime;

    cout<<"Enter time: ";

    getline (cin, ntime);

    cout<<"Corresponding military time is "<
    return 0;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a C program that reads a time from the keyboard. The time should be in the format "HH:MM AM" or "HH:MM PM". Hours can be one or two ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers