C语言字符串分割问题

strtok的函数原型为char *strtok(char *s, char *delim)

#include "stdio.h"
#include "string.h"

char str[20]="2020-3-9 20:19:19";
char *Fir_str,*Sec_str,*year,*month,*day;
int main(int argc, char const *argv[])
{
    Fir_str=strtok(str," ");
    Sec_str=strtok(NULL," ");


    printf("Fir_str:%s
",Fir_str);
    printf("Sec_str:%s
",Sec_str);

    return 0;
}

运行结果:
在这里插入图片描述

#include "stdio.h"
#include "string.h"

char str[20]="2020-3-9 20:19:39";
char *Fir_str,*Sec_str;
char *year,*month,*day,*hour,*minute,*second;
int main(int argc, char const *argv[])
{
    Fir_str=strtok(str," ");
    Sec_str=strtok(NULL," ");
    
    year=strtok(Fir_str,"-");
    month=strtok(NULL,"-");
    day=strtok(NULL,"-");
	
	hour=strtok(Sec_str,":");
    minute=strtok(NULL,":");
    second=strtok(NULL,":");

    printf("Fir_str:%s
",Fir_str);
    printf("Sec_str:%s
",Sec_str);
    printf("year:%s
",year);
    printf("month:%s
",month);
    printf("day:%s
",day);
	
	printf("hour:%s
",hour);
    printf("minute:%s
",minute);
    printf("second:%s
",second);
    return 0;
}

在这里插入图片描述

原文地址:https://www.cnblogs.com/hhsxy/p/14018439.html