Chinese use of Chinese characters, but also mixed with these English, so that we in the number of trouble, the computer is measured in bytes, a Chinese character count two words, and the Chinese custom is a Chinese character is a word, so the computer to calculate must solve the following problems:
Using text control TMemo to store articles, the number of characters in both Chinese and English is counted, we can judge by converting the characters into ASCII values, the ORD () function can convert the characters to corresponding values, the value 33-126 is the keyboard can use the character, the value 127 is the unknown character, is the Chinese character.
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
i,e,c:integer;
begin
s:=memo1.text;
e:=0;c:=0;
for i:=1 to length(s) do
begin
if (ord(s[i])>=33)and(ord(s[i])<=126) then
begin
inc(e);
label1.caption:=英文字数:+inttostr(e);
end
else
if (ord(s[i])>=127) then
begin
inc(c);
label2.caption:=中文字数:+inttostr(c div 2);
end;
end;
end;