64,677
社区成员
发帖
与我相关
我的任务
分享
void ConvertText(const char* pSrc, string& dstStr)
{
int maxCount = 12;
int strSize = int(dstStr.length());
strSize = strSize+1 + (strSize/maxCount);
char* pStrDst = new char[strSize];
int cnt = 0; // 每遇到一个宽字节的字符,cnt++,用以判断cnt的数量是否是2的倍数,即判断是否整齐
for(int i=0,j=0,begin=0; pSrc[j]!='\0'; i++)
{
if ((begin+1)%(maxCount-1) == 0)
{
// j处字节是宽字符
if((unsigned char)pSrc[j] > 0x80)
{
if (cnt%2 == 0)
{
pStrDst[i++] = pSrc[j++];
pStrDst[i++] = pSrc[j++];
pStrDst[i] = '\n';
}
else
{
pStrDst[i++] = pSrc[j++];
pStrDst[i] = '\n';
}
}
// j处字节是普通字符
else
{
// 若j+1处是宽字符,则仅拷贝当前字符,然后插入'\n'
if ((unsigned char)pSrc[j+1] > 0x80)
{
pStrDst[i++] = pSrc[j++];
pStrDst[i] = '\n';
}
// j+1仍然是普通字符,一起拷贝,然后插入'\n'
else
{
pStrDst[i++] = pSrc[j++];
pStrDst[i++] = pSrc[j++];
pStrDst[i] = '\n';
}
}
begin = 0;
cnt = 0; // 重置计数
}
else
{
// 遇到宽字符字节,cnt++
if ((unsigned char)pSrc[j] > 0x80) cnt++;
pStrDst[i] = pSrc[j++];
begin++;
}
}
pStrDst[strSize-1] = '\0';
dstStr = string(pStrDst);
delete [] pStrDst;
}
char* pCh = new char[4];
pCh[0] = 'a';
pCh[1] = 'b';
pCh[2] = 'c';
pCh[3] = '\0';
pCh[4] = 'd';
delete [] pCh;
int main()
{
char *p = new char[2];
p[2] = 0;
delete []p;
}