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\r\n",Fir_str);
    printf("Sec_str:%s\r\n",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\r\n",Fir_str);
    printf("Sec_str:%s\r\n",Sec_str);
    printf("year:%s\r\n",year);
    printf("month:%s\r\n",month);
    printf("day:%s\r\n",day);
	
	printf("hour:%s\r\n",hour);
    printf("minute:%s\r\n",minute);
    printf("second:%s\r\n",second);
    return 0;
}

在这里插入图片描述

发布了14 篇原创文章 · 获赞 8 · 访问量 1702

猜你喜欢

转载自blog.csdn.net/weixin_43739167/article/details/104782781