Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**********Please write using switch cases. and please explain steps to convert s

ID: 3835572 • Letter: #

Question

**********Please write using switch cases. and please explain steps to convert string to int and int back into strings

********************************************************************************************************************************************

2. The format of a date needs to be changed in the following manner: 1st Mar 1984 J> 1984-03-01 Write a function 'ReformatDate" which will take the input String and returns the new format of the string. String Reformat Date(String oldDate) Example 1st Mar 1984 1984 03 01 2nd Feb 2013 2013 02 02 15th Apr 1840 1840 04 15 The "Day" may equal any one of the following values: 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th 29th, 30th, 31st The Month' may equal any one of the following values: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec The Year will always be represented by 4 digits and may equal anything between 1900 and 2100, both inclusive. Your Task is to format the given list of dates in to the following format: YYYY-MM-DD Where: YYYY represents the year in 4 digits MM represents the year in 2 digits DD represents the day in 2 digits

Explanation / Answer

#include <iostream>
#include <cstring>
using namespace std;
string reformatDate(string oldDate)
{
    int day = stoi (oldDate, nullptr, 10);
    int pos = oldDate.find(" ");
    string dayS;
    oldDate = oldDate.substr(pos+1);
    int nextPos = oldDate.find(" ");
    string month = oldDate.substr(0, nextPos);
    string temp = oldDate.substr(nextPos+1);
    int year = stoi(temp, nullptr, 10);
    if(month.compare("Jan") == 0)
        month = "01";
    else if(month.compare("Feb") == 0)
        month = "02";  
    else if(month.compare("Mar") == 0)
        month = "03";  
    else if(month.compare("Apr") == 0)
        month = "04";  
    else if(month.compare("May") == 0)
        month = "05";  
    else if(month.compare("Jun") == 0)
        month = "06";  
    else if(month.compare("Jul") == 0)
        month = "07";  
    else if(month.compare("Aug") == 0)
        month = "08";  
    else if(month.compare("Sep") == 0)
        month = "09";  
    else if(month.compare("Oct") == 0)
        month = "10";  
    else if(month.compare("Nov") == 0)
        month = "11";  
    else if(month.compare("Dec") == 0)
        month = "12";  
    if(day == 1)
        dayS = "01";
    else if(day == 2)
        dayS = "02";
    else if(day == 3)
        dayS = "03";                                                  
    else if(day == 4)
        dayS = "04";
    else if(day == 5)
        dayS = "05";
    else if(day == 6)
        dayS = "06";
    else if(day == 7)
        dayS = "07";
    else if(day == 8)
        dayS = "08";
    else if(day == 9)
        dayS = "09";
    else
        dayS = to_string(day);                          
       
    string output = to_string(year) + " - " + month + " - " + dayS;
    return output;
}
int main()
{
    string dateString;
    cout << "Enter the date (Ex: 1st Mar 1984): ";
    getline(cin, dateString);
    cout << "The reformatted date is: " << reformatDate(dateString) << endl;
}