码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】转变日期格式

时间:2020-09-17 20:28:40      阅读:42      评论:0      收藏:0      [点我收藏+]

标签:dig   cal   pre   mem   col   strcmp   etc   cat   may   

 

char * reformatDate(char * date){
    int len = strlen(date);
    int i,j=0;
    char* str = (char*)calloc(len*2,sizeof(char));
    char* arr[] = {"Jan","01","Feb","02","Mar","03","Apr","04","May","05","Jun","06","Jul","07","Aug","08","Sep","09","Oct","10","Nov","11","Dec","12"};
    for (i=len-1; i>=0; i--)
    {
        char* buff = (char*)calloc(len,sizeof(char));
        if (i && date[i-1] ==  )
        {
            if (isdigit(date[i]))  //对应年份
            {
                strcat(strcat(str,&date[i]),"-");
                date[i-1] = \0;
            }
            else
            {
                for (j=0; j<24; j+=2) //对应月份
                {
                    if (!strcmp(&date[i],arr[j]))
                    {
                        strcat(strcat(str,arr[j+1]),"-");
                        date[i-1] = \0;
                        break;
                    }
                }
            }        
        }
        else if(!i) //对应天数
        {
            if (strlen(date) == 3)
            {
                buff[0] = 0;
                memcpy(&buff[1],date,1);
                strcat(str,buff);
            }
            else
            {
                memcpy(buff,date,2);
                strcat(str,buff);
            }
        }
    }
    return str;
}

 

【leetcode】转变日期格式

标签:dig   cal   pre   mem   col   strcmp   etc   cat   may   

原文地址:https://www.cnblogs.com/ganxiang/p/13644302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!