链接:https://leetcode-cn.com/problems/license-key-formatting/
C++代码:
class Solution {
public:
string licenseKeyFormatting(string S, int K) {
string str = "";
for(int i = 0;i<S.size();i++)
{
if(S[i]!='-')
{
if(S[i]>='a'&&S[i]<='z')
str+=(char)S[i]-32;
else
str+=S[i];
}
}
string res= "";
int reminder = str.size()%K;
if(reminder!=0)
{
res+=str.substr(0,reminder);
res+='-';
}
for(int i = 0;i<str.size()/K;i++)
{
res+=str.substr(reminder+K*i,K);
res+='-';
}
res = res.substr(0,res.size()-1);
return res;
}
};