zoukankan      html  css  js  c++  java
  • 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;
    }
    
    

    在这里插入图片描述

  • 相关阅读:
    P1093 奖学金
    华容道
    回文数
    P1654 OSU!
    Noip P1063 能量项链
    Noip 寻宝
    NOIP 2009 普及组 第三题 细胞分裂
    拦截器
    OGNL
    Struts2 配置详解
  • 原文地址:https://www.cnblogs.com/hhsxy/p/14018439.html
Copyright © 2011-2022 走看看