Determination of validity of ID card numbers

Source: Internet
Author: User
Problem description:
The ID card numbers of Chinese citizens are characterized as follows:
1. The length is 18 characters;
2. 1st ~ 17 digits can only be numbers;
3. 18th digits can be digits or lowercase letters X.
4. ID card number 7th ~ 14 digits indicate the year, month, and day of the holder's birthday.

For example, 511002198808080111 or 0000002198808011x.
 
Implement the function of determining the validity of the ID card number. In addition to meeting the above requirements, the applicant's birthday information should be verified. The year is later than or equal to 1900, and less than or equal to 2100. You need to consider the leap year, the size of the month. The year in which a leap year can be divided by four and cannot be divided by 100 or 400. The February of a leap year is 29 days, and the February of a non-leap year is 28 days. In other cases, you do not need to consider the legitimacy verification.
Function return value:
1. If the ID card number is valid, return 0;
2. if the length of the ID card number is invalid, return 1;
3. If the ID card number is 1st ~ Returns 2 If the 17 characters contain non-numeric characters;
4. If the ID card number 18th is neither a number nor an English lowercase letter X, 3 is returned;
5. If the annual information of the ID card number is invalid, return 4;
6. If the monthly ID card number is invalid, return 5;
7. If the day information of the ID card number is invalid, 6 is returned (note the situation of leap year );
Note: Except for successful cases, the priority of the above legitimacy judgments is reduced in turn. That is to say, if the length is determined invalid, 1 is returned directly, and no other validity judgment is required.
 
Required implementation functions:
Int verifyidcard (char * input)

Example
1. Input: "511002111222", function return value: 1;
2. Input: "0000002abc123456789", function return value: 2;
3. Input: "000000219880808123a", function return value: 3;
4. Input: "511002188808081234", function return value: 4;
5. Input: "511002198813081234", function return value: 5;
6. Input: "511002198808321234", function return value: 6;
7. Input: "511002198902291234", function return value: 6;

8. Input: "511002198808081234", function return value: 0;


The C code is as follows:

#include<stdio.h>#include<stdlib.h>#include<string.h>int verifyIDCard(char* input);int is_leapyear(int year);int getyear(char* input);int getmonth(char* input);int getday(char* input);int check_1to17(char* input);int check18(char* input);int chech_year_month_day(int year, int month, int day);int main(){char input[8][100] = {"511002111222","511002abc123456789","51100219880808123a","511002188808081234","511002198813081234","511002198808321234","511002198902291234","511002198808081234"};int i;for(i=0; i<8; i++){printf("the IDcard is: %s, the result is: %d\n",input[i],verifyIDCard(input[i]));}return 0;}int is_leapyear(int year){if(( (year%4==0) && (year%400!=0) ) || (year%400==0))return 1;return 0;}int getyear(char* input){int year=0;year = (input[6]-'0') * 1000 + (input[7]-'0') * 100 +(input[8]-'0') * 10 + (input[9]-'0');return year;}int getmonth(char* input){int month=0;month = (input[10]-'0') * 10 + (input[11]-'0');return month;}int getday(char* input){int day=0;day = (input[12]-'0') * 10 + (input[13]-'0');return day;}int check_1to17(char* input){int i;for(i=0; i<17; i++)if( !(input[i]>='0' && input[i]<='9') )return 0;return 1;}int check18(char* input){char ch = input[17];if( (ch=='x') || (ch>='0' && ch<='9') )return 1;elsereturn 0;}int check_year_month_day(int year, int month, int day){int leap;leap = is_leapyear(year);if(year<1900 || year>2100)return 4;if(month<1 || month>12)return 5;switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:{if(day<1 || day>31)return 6;break;}case 4:case 6:case 9:case 11:{if(day<1 || day>30)return 6;break;}case 2:{if(leap){if(day<1 || day>29)return 6;break;}else{if(day<1 || day>28)return 6;break;}}}return 0;}int verifyIDCard(char* input){int year,month,day;if(strlen(input)!=18)return 1;else{if(!check_1to17(input))return 2;else{ if(!check18(input)) return 3; else {year = getyear(input);month = getmonth(input);day = getday(input);return check_year_month_day(year,month,day); }}}}

The running result is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.