China's mainland operators of mobile phone number standard format is: Country code + mobile phone number, for example: 8613912345678. Features are as follows:
1. length 13;
2. Begin with 86 of the country code;
3. Each digit of the mobile phone number is a number.
4. Mobile phone numbers such as the following are qualified: +86-13957177889, 8613957177889, +8613957177889, +86-139-5717-7889, 86-13957177889.
Please implement the function of judging the legality of mobile phone number (note: Candidates do not need to pay attention to the authenticity of mobile phone number, that is, such as 86123123456789 mobile phone number, we also believe that is legal), requirements:
1. If the mobile number is qualified, return 0;
2. If the phone number is not valid, return 1;
3. If the phone number contains non-numeric characters, return 2;
4. If the mobile phone number does not begin with 86, return 3;
5. In other cases, return 4.
Note: In addition to the success of the case, the above other legal judgment priority is reduced once, that is, if the length is determined to be illegal, directly return 1, do not need to do other legal judgment.
<span style= "font-family:kaiti_gb2312;" > Code:</span> #include <stdio.h> #include <string.h>bool be (char str[])//start with ' + ' to return 1 {if (str[0]== ' + ') ) return 1;return 0;} BOOL _86 (char Str[],int len)//whether to start with 86 {if (BE (str)) {if (str[1]== ' 8 ' &&str[2]== ' 6 ') return 1;elsereturn 0;} Else{if (str[0]== ' 8 ' &&str[1]== ' 6 ') return 1;elsereturn 0;}} int Len (char str[],int Len) {int i,k=0;for (i=0;i<len;++i) {if (str[i]>= ' 0 ' &&str[i]<= ' 9 ') k++;} return k;} BOOL _fsz (char Str[],int len)//To determine whether a non-numeric is included, is to return 1; {int i;if (is (str)) for (I=1;i<len;++i) {if (str[i]!= '-' &&! ( str[i]>= ' 0 ' &&str[i]<= ' 9 ')) return 1; Else{for (i=0;i<len;++i) {if (str[i]!= '-' &&! ( str[i]>= ' 0 ' &&str[i]<= ' 9 ')) return 1; }}return 0;} int MAIN () {char str[20];gets (str), int len=strlen (str), switch (len) {case 13:if (_86 (Str,len)) return 0;//else break; 14:if (_86 (Str,len)) if (BE (str) | | str[2]== '-') return 0;case 15:if (_86 (Str,len)) if (Be (str) &&str[3]== '-') RetuRN 0;case 17:if (_86 (Str,len)) if (Be (str) &&str[3]== '-' &&str[7]== '-' &&str[12]== '-') return 0;} if (Len (Str,len)!=13)//Length unqualified return 1;else{if (_fsz (Str,len)) return 2;else{if (_86 (Str,len)) return 4;elsereturn 3;}}} int main () {while (1) printf ("%d\n", Main ()); return 0;}
Country code + mobile phone number