关于C++对汉字拼音的处理(2)

对于前面获取字符串汉字全拼音的功能,大家应该有个了解了。现在我又综合广大网友流传的获取字符串汉字拼音首字母的功能进行了整理。介绍如下

这个功能写的稍微有点复杂 使用3个函数解决了获取字符串汉字首拼音串的问题。

代码如下:

 1 bool GetChineseCodeGBK(const char *pChineseCharacter, char *pGBK, const size_t nChineseCharacter = 3, const size_t nGBK = 5)
 2 {
 3     bool is_success = false;
 4 
 5     do 
 6     {
 7         int len = sprintf_s(pGBK, nGBK, "%X%X", (unsigned char)pChineseCharacter[0], (unsigned char)pChineseCharacter[1]);
 8         if (len<0)
 9             return is_success = false;
10         else
11             return is_success = true;
12     } while (false);
13 
14     return is_success;
15 }
 1 char GetFirstCharacter(const char *pGBK, const size_t nGBK = 5)
 2 {
 3     int nCode2 = atol(pGBK);//error
 4     int nCode = strtol(pGBK, NULL, 16);//ok
 5     int nCode3;
 6     sscanf_s(pGBK, "%x",&nCode3); //ok
 7     int areacode[] = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481, 55290 };
 8     for (int i = 0; i < 26; i++)
 9     {
10         if (areacode[i] <= nCode && nCode < areacode[i + 1])
11         {
12             return (char)('A'+i);
13         }
14     }
15     return (char)0;
16 }
 1 std::string get_first_pinyin_string(const std::string &strHanzi)
 2 {
 3     std::string retFistPinyinString;
 4     char retFistPinyin;
 5 
 6 
 7     if (strHanzi.empty() == true)
 8     {
 9         return 0;
10     }
11 
12     for (size_t i = 0; i < strHanzi.length(); ++i)
13     {
14         char GBK[5] = { 0 };
15 
16         if (static_cast<unsigned char>(strHanzi[i]) < 0x80)
17         {
18             retFistPinyinString += strHanzi[i];
19             continue;
20         }
21 
22         if (GetChineseCodeGBK(&strHanzi[i], GBK))
23         {
24             retFistPinyin = GetFirstCharacter(GBK);
25             if ((char)0 != retFistPinyin)
26             {
27                 retFistPinyinString += retFistPinyin;
28                 ++i;
29             }
30         }
31     }
32 
33 
34     return retFistPinyinString;
35 }

下面是测试代码

 1 int _tmain(int argc, _TCHAR* argv[])
 2 {
 3 
 4     std::string str_test10("任晓霞l2;哈哈哈mu男人");
 5     std::string str_test10_rt;
 6     str_test10_rt = get_first_pinyin_string(str_test10);
 7 
 8     
 9 
10     return 0;
11 }

有兴趣的朋友可以试试啦。

原文地址:https://www.cnblogs.com/superstargg/p/4378743.html