C++将用户名部分用*代替
简要:很多时候中奖用户并不希望让别人知道他的ID。程序中我们就将他们的账号部分设置为*号显示。
例如:王小二->王*二;
asadjsahd->a*********d;
代码:
#include<string.h> #include<stdlib.h> #define toStar(str,start,end) \ do{ \ for(int i=start;i<end;i++)\ { \ str[i] = '*';\ } \ }while(0) //GBK const char* toStarString(const char* username) { int len = strlen(username); char *starName = new char[len+1]; strcpy(starName,username); int start = 1; //默认保留头尾字符 int end = len - 1; unsigned char ch = starName[0]; //取头字符 //if(isalnum(ch)) {toStar(starName,3,len-3);} if ( ch >= 0xB0 && ch <= 0xF7 ) //判断头是否为汉字 { //继续检查下一个字符是否能组织成汉字 unsigned char ch1=starName[1]; if ( ch1 >=0xA1 && ch1 <= 0xFE ) { start = 2; } } ch = starName[len-1]; //尾字符 if(ch >=0xA1 && ch <= 0xFE) { unsigned char ch1=starName[len-2]; if ( ch1 >= 0xB0 && ch1 <= 0xF7 ) { end = len - 2; } } if(start<=end-1) toStar(starName,start,end); else toStar(starName,end,len); return starName; } //Unicode const wchar_t* toStarUn(const wchar_t* username) { int len = wcslen(username); wchar_t *starName = new wchar_t[len+1]; wcscpy(starName,username); if(len>2) toStar(starName,1,len-1); else if(len == 2) toStar(starName,1,len); return starName; }