从身份证中提取出去年份

随便输入一串身份证提取出生年份
例如输入366532200206053653
输出2002

遍历字符串取第7到第10个字符。

img

首先对于输入的字符串要检查其长度是否为18,否则身份证都是错的,哪来的年份
然后根据身份证格式,截取第6-9个字符就是年份

#include <string.h>
int main()
{
    char id[30] = {0};
    scanf("%s",id);
    int len = strlen(id);
    if(len == 18)
    {
          id[10] = 0;
          printf("%s",id+6);
    }
    else
          printf("无效身份证号。");
    return 0;
}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
    string s;
    cin>>s;
    for(int i = 6; i < 10; i++)
    {
        cout<<s[i];
    }
    cout<<endl;
    return 0;
}