How to resolve the algorithm Validate International Securities Identification Number step by step in the C++ programming language
How to resolve the algorithm Validate International Securities Identification Number step by step in the C++ programming language
Table of Contents
Problem Statement
An International Securities Identification Number (ISIN) is a unique international identifier for a financial security such as a stock or bond.
Write a function or program that takes a string as input, and checks whether it is a valid ISIN. It is only valid if it has the correct format, and the embedded checksum is correct. Demonstrate that your code passes the test-cases listed below.
The format of an ISIN is as follows:
For this task, you may assume that any 2-character alphabetic sequence is a valid country code. The checksum can be validated as follows:
(The comments are just informational. Your function should simply return a Boolean result. See #Raku for a reference solution.)
Related task:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Validate International Securities Identification Number step by step in the C++ programming language
This C++ code is designed to check the validity of International Securities Identification Numbers (ISINs) using two helper functions: CheckFormat
and CodeISIN
, and then validates the ISIN using the Luhn algorithm in the CkeckISIN
function. Here's a breakdown of the code:
-
CheckFormat
function:- It takes an ISIN as input and uses a regular expression (
std::regex
) to check if the ISIN follows a predefined format. - A valid ISIN must consist of 12 characters, starting with two alphabetic characters followed by nine alphanumeric characters and ending with a single numeric character.
- The function returns
true
if the ISIN matches the expected format andfalse
otherwise.
- It takes an ISIN as input and uses a regular expression (
-
CodeISIN
function:- It takes an ISIN as input and converts each character to a corresponding numeric value.
- Alphabetic characters are converted to their ASCII numeric equivalents (e.g., 'A' becomes '10'), while non-alphabetic characters are left unchanged.
- The modified ISIN, with alphabetic characters converted to numbers, is then returned.
-
CkeckISIN
function:- It first calls
CheckFormat
to ensure the ISIN has the correct format. - If the format is valid, it calls
CodeISIN
to convert the ISIN to a numeric string. - The numeric string is then validated using the Luhn algorithm, a common checksum method used to detect errors in identification numbers.
- The function returns
true
if the ISIN passes the Luhn check andfalse
otherwise.
- It first calls
-
main
function:- An array of ISINs (
isins
) is defined for testing. - For each ISIN, the function calls
CkeckISIN
to check its validity and prints the result along with the ISIN. - Output:
- The program outputs the ISINs and their validation status (true or false) for each ISIN in the test array.
- An array of ISINs (
Source code in the cpp programming language
#include <string>
#include <regex>
#include <algorithm>
#include <numeric>
#include <sstream>
bool CheckFormat(const std::string& isin)
{
std::regex isinRegEpx(R"([A-Z]{2}[A-Z0-9]{9}[0-9])");
std::smatch match;
return std::regex_match(isin, match, isinRegEpx);
}
std::string CodeISIN(const std::string& isin)
{
std::string coded;
int offset = 'A' - 10;
for (auto ch : isin)
{
if (ch >= 'A' && ch <= 'Z')
{
std::stringstream ss;
ss << static_cast<int>(ch) - offset;
coded += ss.str();
}
else
{
coded.push_back(ch);
}
}
return std::move(coded);
}
bool CkeckISIN(const std::string& isin)
{
if (!CheckFormat(isin))
return false;
std::string coded = CodeISIN(isin);
// from http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#C.2B.2B11
return luhn(coded);
}
#include <iomanip>
#include <iostream>
int main()
{
std::string isins[] = { "US0378331005", "US0373831005", "U50378331005",
"US03378331005", "AU0000XVGZA3", "AU0000VXGZA3",
"FR0000988040" };
for (const auto& isin : isins)
{
std::cout << isin << std::boolalpha << " - " << CkeckISIN(isin) <<std::endl;
}
return 0;
}
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Racket programming language
You may also check:How to resolve the algorithm Shortest common supersequence step by step in the Elixir programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the Scala programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Delphi programming language