汉字取首字母

汉子取首字母

在很多软件中,输入拼音的首写字母就可以快速定位到某个词条。比如,在铁路售票软件中,输入: “bj”就可以定位到“北京”。怎样在自己的软件中实现这个功能呢?问题的关键在于:对每个汉字必须能计算出它的拼音首字母。

GB2312汉字编码方式中,一级汉字的3755个是按照拼音顺序排列的。我们可以利用这个特征,对常用汉字求拼音首字母。

GB2312编码方案对每个汉字采用两个字节表示。第一个字节为区号,第二个字节为区中的偏移号。为了能与已有的ASCII编码兼容(中西文混排),区号和偏移编号都从0xA1开始。

我们只要找到拼音a,b,c,...x,y,z 每个字母所对应的GB2312编码的第一个汉字,就可以定位所有一级汉字的拼音首字母了(不考虑多音字的情况)。下面这个表给出了前述信息。请你利用该表编写程序,求出常用汉字的拼音首字母。


a 啊 B0A1
b 芭 B0C5
c 擦 B2C1
d 搭 B4EE
e 蛾 B6EA
f 发 B7A2
g 噶 B8C1
h 哈 B9FE
j 击 BBF7
k 喀 BFA6
l 垃 C0AC
m 妈 C2E8
n 拿 C4C3
o 哦 C5B6
p 啪 C5BE
q 期 C6DA
r 然 C8BB
s 撒 C8F6
t 塌 CBFA
w 挖 CDDA
x 昔 CEF4
y 压 D1B9
z 匝 D4D1


【输入、输出格式要求】

用户先输入一个整数n (n<100),表示接下来将有n行文本。接着输入n行中文串(每个串不超过50个汉字)。

程序则输出n行,每行内容为用户输入的对应行的汉字的拼音首字母。

字母间不留空格,全部使用大写字母。

例如:
用户输入:
3
大家爱科学
北京天安门广场
软件大赛

则程序输出:
DJAKX
BJTAMGC
RJDS


#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;

int table[27] =
 {0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0xBBF7,0xBFA6,0xC0AC,

    0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA,0xCDDA,0xCDDA,0xCEF4,0xD1B9,0xD4D1,0xFFFF};
//注意题中没有关于i,u,v开头的拼音(因为拼音中没有以i,u,v开头的),所以用下一个值代替即可,最后边界用极大值代替

int search(int p)
{
    for(int i = 0; i < 27; i++){
        if(table[i] > p)
            return (i-1);
    }
}

int main()
{
    int N;
    cin >> N;
    while(N--){
        char buf[110];
        cin >> buf;
        int len = strlen(buf);//一个汉子占两个字节
        for(int i = 0; i < len; i += 2){
            int tmp1 = 0x000000ff & buf[i];//取出第一个字节,因为tmp为int型,有32位,所以只要取出后八位即可
            int tmp2 = 0x000000ff & buf[i+1];//取出第二个字节
            int pos = search(tmp1 * 0x100 + tmp2);//合并操作   tmp*0x100意味着将tmp1二进制每个位左移8位,与tmp2相加合并
            char c = pos + 'A';
            cout << c;
        }
        cout << '\n';
    }
    return 0;
}


PHP可以使用以下代码来汉字首字母: ```php function getFirstLetter($str){ $result = ''; for ($i=0; $i < mb_strlen($str, 'utf8'); $i++) { $result .= getFirstChar(mb_substr($str, $i, 1, 'utf8')); } return $result; } function getFirstChar($str){ $fchar = ord(substr($str, 0, 1)); if ($fchar >= ord("A") and $fchar <= ord("Z") ) return $str; $asc = ord($str[0])*256 + ord($str[1])-65536; if ($asc >= -20319 and $asc <= -20284) return "A"; if ($asc >= -20283 and $asc <= -19776) return "B"; if ($asc >= -19775 and $asc <= -19219) return "C"; if ($asc >= -19218 and $asc <= -18711) return "D"; if ($asc >= -18710 and $asc <= -18527) return "E"; if ($asc >= -18526 and $asc <= -18240) return "F"; if ($asc >= -18239 and $asc <= -17923) return "G"; if ($asc >= -17922 and $asc <= -17418) return "H"; if ($asc >= -17417 and $asc <= -16475) return "J"; if ($asc >= -16474 and $asc <= -16213) return "K"; if ($asc >= -16212 and $asc <= -15641) return "L"; if ($asc >= -15640 and $asc <= -15166) return "M"; if ($asc >= -15165 and $asc <= -14923) return "N"; if ($asc >= -14922 and $asc <= -14915) return "O"; if ($asc >= -14914 and $asc <= -14631) return "P"; if ($asc >= -14630 and $asc <= -14150) return "Q"; if ($asc >= -14149 and $asc <= -14091) return "R"; if ($asc >= -14090 and $asc <= -13319) return "S"; if ($asc >= -13318 and $asc <= -12839) return "T"; if ($asc >= -12838 and $asc <= -12557) return "W"; if ($asc >= -12556 and $asc <= -11848) return "X"; if ($asc >= -11847 and $asc <= -11056) return "Y"; if ($asc >= -11055 and $asc <= -10247) return "Z"; return $str; } $hanzi = '你好'; echo getFirstLetter($hanzi); // 输出:NH ``` 以上代码是通过调用`getFirstLetter`函数来遍历输入的汉字字符串,然后调用`getFirstChar`函数来获每个汉字首字母。通过这样的方式,可以将汉字转换为拼音首字母
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值