【leetcode】转变日期格式

技术文章 1年前 (2020) 完美者
2,030 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

版权声明:完美者 发表于 2020-09-17 20:28:40。
转载请注明:【leetcode】转变日期格式 | 完美导航

暂无评论

暂无评论...