#include <iostream>
#include <string>
using namespace std;
string formatString(const string& str, int segWidth, char delimiter)
{
size_t size = str.size();
size_t pos = 0;
string result;
if (size > 0 && segWidth > 0 && size > segWidth) {
while(pos < size) {
result += str.substr(pos, segWidth);
pos += segWidth;
if (pos < size) {
result += delimiter;
}
//cout << result<<endl;
}
}
else {
result = str;
}
return result;
}
int main(int argc, char* argv[])
{
string s1 = "a3dfae18d0dba9027e56591122f3ef84";
string s2 = formatString(s1, 4, '-');
cout << "s1: " << s1 << "\ns2: " << s2<<endl;
return 0;
}
output:
s2: a3df-ae18-d0db-a902-7e56-5911-22f3-ef84