How to validate IFSC Code using Regular Expression in C++
Hello fellow learners! In this tutorial, We are going to learn about how to validate IFSC Code using Regular Expression in C++. Now let’s learn a little about IFSC code.
IFSC (Indian Financial System) Code is known as alphanumeric code which is 11- a digit code that is mostly used in banks for the purpose of electronic funds. So, a valid IFSC code should fulfill some conditions.
The conditions are as listed below:
- 11 characters should be there.
- First 4 characters = upper case alphabets.
- Fifth character = 0
- Last character = Number/alphabets(sometimes).
Let’s see an example here for an IFSC code:
- CNRB0000301
- SBIN0040638
Algorithm:
- Create a string.
- After that regular expression will be created to check whether a string is an IFSC code or not. For this, we will use regex expression in C++. Regex is a regular expression that has a sequence of characters or numbers that will have a particular pattern and can be used where pattern formation is required in C++.
regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
The table here represents the symbols used in regex expression and their meaning.
SYMBOLS | MEANING | |
^ | String starts | |
[A-Z]{4} | First 4 upper case alphabets. | |
0 | Fifth should be 0. | |
[A-Z0-9]{6} | next six characters are numeric or alphabetic. | |
$ | String ends |
3. If the string is null, return false.
4. If the string is IFSC it validates the regex function and the return true.
5. In driver code, give examples / test cases to check whether the string is IFSC code or not.
6. If the given code is IFSC the output will be 1,if the given code is not IFSC it will return 0.
Note: Don’t forget to mention the regex expression header file in the code.
C++ Code: Validate IFSC code using Regular Expression
Read the comments in the code for better understanding.
#include <iostream> #include<regex> using namespace std; bool isValidifscodeyorn(string str)// Function to check IFSC Code. { const regex pattern("^[A-Z]{4}0[A-Z0-9]{6}$"); // to check IFSC code if (str.empty()) // if empty return false { return false; } if(regex_match(str, pattern)) // if ifsc matches the regex expression return true. { return true; } else { return false; } } // Driver Code int main() { string str1 = "JNUN012562B"; cout << isValidifscodeyorn(str1) << endl; string str2 = "KAUS0195"; cout << isValidifscodeyorn(str2) << endl; string str3 = "PUNB0150100"; cout << isValidifscodeyorn(str3) << endl; string str4 = "ZXCN798562x"; cout <<isValidifscodeyorn(str4) << endl; return 0; }
Output: 1 0 1 0
Hope this tutorial was useful.
Leave a Reply