【leetcode】密钥格式化

 

char * licenseKeyFormatting(char * S, int K){
    int i,j=0,count=0,pst=0,n=0;
    int len = strlen(S);
    char* str = (char*)calloc(len*2,sizeof(char));
    char* retStr = (char*)calloc(len*2,sizeof(char));    
    for (i=0; i<len; i++)
    {
        if (S[i] != '-')
        {
            if (S[i]>=97 && S[i]<=122) S[i] -= 32;
            str[pst++]=S[i];
        }
    }
     count = pst % K;
     if (count == 0) count = K;
    while(*str)
    {
        n++;
        if (n == count+1)
        {
            retStr[j++] = '-';
            n=0;
            count=K;
            continue;
        }
        retStr[j++] = *str++;
    }
    return retStr;
}

 

posted @ 2020-09-23 09:19  温暖了寂寞  阅读(195)  评论(0编辑  收藏  举报