May 2018
Intermediate to advanced
328 pages
8h 15m
English
The simplest way to solve this problem is by using regular expressions. The regular expression that meets the described format is "[A-Z]{3}-[A-Z]{2} \d{3,4}".
The first function only has to validate that an input string contains only text that matches this regular expression. For that, we can use std::regex_match(), as follows:
bool validate_license_plate_format(std::string_view str){ std::regex rx(R"([A-Z]{3}-[A-Z]{2} \d{3,4})"); return std::regex_match(str.data(), rx);}int main(){ assert(validate_license_plate_format("ABC-DE 123")); assert(validate_license_plate_format("ABC-DE 1234")); assert(!validate_license_plate_format("ABC-DE 12345")); assert(!validate_license_plate_format("abc-de 1234"));}
The second function ...
Read now
Unlock full access