C++
vector<string> split(string str, char delimiter)
{
stringstream ss(str);
string element;
vector<string> split_str;
while(getline(ss, element, delimiter))
{
split_str.push_back(element);
}
return split_str;
}
string convert(string date)
{
vector<string>split_date = split(date, '-');
string formattedDate;
string year, month, day;
if(split_date[0].length() == 4)
{
year = split_date[0];
month = split_date[1];
day = split_date[2];
}
else
{
month = split_date[0];
day = split_date[1];
year = split_date[2];
}
if(year.length() == 2)
{
year = "20" + year;
}
if(month.length() < 2 && atoi(month.c_str()) < 10)
{
month = '0' + month;
}
if(day.length() < 2 && atoi(day.c_str()) < 10)
{
day = '0' + day;
}
formattedDate += (year + '-' + month + '-' + day);
return formattedDate;
}
int main(int argc, const char * argv[]) {
if(argc != 2 )
{
cout << "Program requires one argument (a filename) to run." << endl;
}
else
{
ifstream infile;
infile.open(argv[1]);
if(!infile.is_open())
{
cout << "Error: file not opened." << endl;
}
else
{
string line;
vector<string> lines;
while(getline(infile, line))
{
string cleanedLine;
for(int i = 0; i < line.length(); i++)
{
if(line[i] == ' ' || line[i] == '-' || line[i] == '/')
{
cleanedLine += '-';
}
else
{
cleanedLine += line[i];
}
}
lines.push_back(cleanedLine);
}
infile.close();
for(int i = 0; i < lines.size(); i++)
{
string formattedDate = convert(lines[i]);
cout << formattedDate << endl;
}
}
}
return 0;
}