License Key Formatting LeetCode solution in C++
1class Solution {2public:3string licenseKeyFormatting(string S, int K) {4stack<char> sChars;5for (auto ch : S) {6if (ch != '-') sChars.push(ch);7}89string sRet;10int cur = 0;11while (!sChars.empty()) {12sRet += toupper(sChars.top());13sChars.pop();14cur++;1516if (cur == K && !sChars.empty()) {17sRet += '-';18cur = 0;19}20}21reverse(sRet.begin(), sRet.end());22return sRet;23}24};