-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmericanFormat.cpp
45 lines (31 loc) · 1.13 KB
/
AmericanFormat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
using namespace std;
int main() {
string Date;
cout << "Enter the date in American Format" << endl;
cout << "Format: Month Day, Year\n";
cout << "Example: March 19, 2005\n\n\n";
cout << "Enter date: ";
getline(cin, Date, '\n');
int len = Date.length();
cout << "Date length is: " << len << " (Positions 0 --> "
<< len - 1 << ")" << endl;
// Extract the month
int i = Date.find(" "); // Return position where space occurs
cout << "Space occurs at position = " << i << endl;
string Month = Date.substr(0, i); // start at P0, i characters long
cout << "The month is: " << Month << endl;
// Extract the day
int k = Date.find(',');
string Day = Date.substr(i + 1, k - i - 1);
cout << "Comma occurs at position = " << k << endl;
cout << "The day is: " << Day << endl;
// Extract the year
string Year = Date.substr(k + 2, (Date.size()) - k - 2);
cout << "The year is: " << Year << endl;
string newDate = Day + " " + Month + " " + Year;
cout << "Original Date: " << Date << endl;
cout << "Converted Date: " << newDate << endl;
return 0;
}