Friday, 27 May 2022

Chapter 23 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 23 // Exercise 12

Write a program, based on the program that finds lines containing dates (exercise 6), that finds all dates and reformats them to the ISO yyyy-mm-dd format. The program should take an input file and produce and output file that is identical to the input file except for the changed date formatting.


Had a look for a replace type regex and found the standard libraries regex_replace()
This will replace all matches it finds on the same line in one go. Amazing.

I then went off down a tangent using substring to replace certain parts of the string, then attempted to convert the string to time_t to convert the time_t to a particular format. I then bashed my head against the wall and simply pushed the 3 dates into a vector then concatenated them into whatever format I wanted.

This exercise took me longer than expected as C# regex can simply replace one date format with another in 1 line....if only.

To save some time I decided not to do ALL dates and changed only the mm/dd/yyyy and dd/mm/yyyy formats.

No comments:

Post a Comment