1 #include <stdint.h>
2 #include <stdio.h>
3 #include <ctype.h>
4 #include <string.h>
5
6 bool between(uint32_t start,uint32_t end,uint32_t aim);
7 char get_first_letter(wchar_t wchar);
8 void get_first_letters(const char *szChinese,char piyinBuf[],const uint32_t maxBufLen,uint32_t &piyinBufLen);
9
10
11 int32_t main()
12 {
13 char buf[1024] = {0};
14 uint32_t len = 0;
15 const char *szChinese = "qaz123获取汉字拼音";
16 get_first_letters(szChinese,buf,1024,len);
17 printf("%s\n",buf);
18 return 0;
19 }
20
21 void get_first_letters(const char *szChinese,char piyinBuf[],const uint32_t maxBufLen,uint32_t &piyinBufLen)
22 {
23 uint8_t chr[3];
24 piyinBufLen = 0;
25 uint32_t chineseLen = strlen(szChinese);
26 for (uint32_t i = 0; i < chineseLen && piyinBufLen < maxBufLen; ++i)
27 {
28 uint8_t c = szChinese[i];
29 // 排除askii 码
30 if (isascii(c))
31 {
32 piyinBuf[piyinBufLen++] = c;
33 }
34 else
35 {
36 chr[0] = szChinese[i];
37 chr[1] = szChinese[++i];
38 chr[2] = 0;
39 wchar_t wchr = 0;
40 wchr = (chr[0] & 0xff) << 8;
41 wchr |= (chr[1] & 0xff);
42 piyinBuf[piyinBufLen++] = toupper(get_first_letter(wchr));
43 }
44 }
45 }
46
47 bool between(wchar_t start,wchar_t end,wchar_t aim)
48 {
49 if (start <= aim && aim <= end)
50 {
51 return true;
52 }
53 return false;
54 }
55
56 char get_first_letter(wchar_t wchar)
57 {
58 if (between(0xB0A1,0xB0C4,wchar)) return 'a';
59 if (between(0XB0C5,0XB2C0,wchar)) return 'b';
60 if (between(0xB2C1,0xB4ED,wchar)) return 'c';
61 if (between(0xB4EE,0xB6E9,wchar)) return 'd';
62 if (between(0xB6EA,0xB7A1,wchar)) return 'e';
63 if (between(0xB7A2,0xB8c0,wchar)) return 'f';
64 if (between(0xB8C1,0xB9FD,wchar)) return 'g';
65 if (between(0xB9FE,0xBBF6,wchar)) return 'h';
66 if (between(0xBBF7,0xBFA5,wchar)) return 'j';
67 if (between(0xBFA6,0xC0AB,wchar)) return 'k';
68 if (between(0xC0AC,0xC2E7,wchar)) return 'l';
69 if (between(0xC2E8,0xC4C2,wchar)) return 'm';
70 if (between(0xC4C3,0xC5B5,wchar)) return 'n';
71 if (between(0xC5B6,0xC5BD,wchar)) return 'o';
72 if (between(0xC5BE,0xC6D9,wchar)) return 'p';
73 if (between(0xC6DA,0xC8BA,wchar)) return 'q';
74 if (between(0xC8BB,0xC8F5,wchar)) return 'r';
75 if (between(0xC8F6,0xCBF0,wchar)) return 's';
76 if (between(0xCBFA,0xCDD9,wchar)) return 't';
77 if (between(0xCDDA,0xCEF3,wchar)) return 'w';
78 if (between(0xCEF4,0xD188,wchar)) return 'x';
79 if (between(0xD1B9,0xD4D0,wchar)) return 'y';
80 if (between(0xD4D1,0xD7F9,wchar)) return 'z';
81 return '\0';
82 }
原文链接: https://www.cnblogs.com/tangxin-blog/p/5540622.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/234187
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!