Microsoft Interview Question
Software Engineer in Testschar* ampm = new char[2];
char* str = new char[15];
strcpy(str,"12:52 PM");
sscanf(str,"%d:%d %s" ,&hr,&min,ampm);
int minToAdd = 20;
min = min + minToAdd;
if (min > 60)
{
min = min - 60;
if (hr == 12)
hr = 1;
else
{
if (hr == 11)
{
if (!strcmp(ampm,"AM"))
strcpy(ampm,"PM");
else
strcpy(ampm,"AM");
}
hr = hr + 1;
}
}
sprintf(str,"%d:%d%d %s",hr,min/10,min%10,ampm);
i am giving a solution here, don't know whether it is upto the expectation of MS or not:
- Anonymous July 01, 2008#include<iostream>
#include<string.h>
using namespace std;
int main(int argc, char *argv[])
{
char *str, *s;
int hours, minutes, n;
if(argc!=3)
{
cout<<"Enter in format:: ""HH:MM AM|PM"" INT<1 - 60>";
}
str = strtok(argv[1],": ");
hours = atoi(str);
str = strtok(NULL,": ");
minutes = atoi(str);
str = strtok(NULL,": ");
s = str;
n = minutes + atoi(argv[2]);
if(n > 60)
{
if(hours == 12)
{
hours = 1;
if(s = "AM")
s = "PM";
else
s = "AM";
}
else
hours++;
minutes = n - 60;
cout.fill('0');
cout.width(2);
cout<<hours<<":";
cout.fill('0');
cout.width(2);
cout<<minutes<<" "<<s<<endl;
}
cout<<endl;
system("PAUSE");
return 0;
}
Run as:
<filename.exe> "HH:MM AM|PM" 13