Skip to content

Instantly share code, notes, and snippets.

@udovicic
Last active August 29, 2015 14:00
Show Gist options
  • Save udovicic/2a1bb068673711f5fbef to your computer and use it in GitHub Desktop.
Save udovicic/2a1bb068673711f5fbef to your computer and use it in GitHub Desktop.
/* Test IBAN values:
HR 15 2340 0093 2109 5545 7
HR 5023400093102278840
*/
#include <iostream>
#include <string>
#include <algorithm> // needed for remove_if
using namespace std;
bool isValid(string iban)
{
// trim spaces
iban.erase( std::remove_if( iban.begin(), iban.end(), ::isspace ), iban.end() );
// move country code and control number to back
int len = iban.length();
string code = iban.substr(4, len - 4) + "1727" + iban.substr(2, 2);
int n=0, // current operating number
r=0, // result of division
l=0; // reminder
while (code.length() > 0) {
// append next digit to remider of last division
string digit = code.substr(0,1);
n = l*10 + atoi(digit.c_str());
// drop first digit
code = code.substr(1, code.length() -1);
r = n/97;
l = n%97;
// number is smaller than 97, try appending next digit
if (r != 0) n=0;
}
return (l == 1) ? true : false;
}
int main(int argc, char *argv[])
{
string iban;
cout << "Input IBAN: ";
getline(cin, iban);
if (isValid(iban) == true) {
cout << "Entered IBAN is valid" << endl;
} else {
cout << "Entered IBAN is not valid" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment