检查字符是否为换行符

时间:2012-05-15 10:01:23

标签: delphi pascal

你好我有一个简单的程序,它是在给定文本中couting字符,直到行只有新行的空行

var
  znaki: array['a'..'z'] of integer = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  napis: String;
  maly: String;
  dlugosc: Integer;
  znak: char;


begin
napis := 'a';
while napis[1] <> '#13#10'do
begin
  readln(napis);
  maly:=LowerCase(napis);
  for dlugosc:=(length(maly)) downto 1 do
begin
znaki[maly[dlugosc]]:=znaki[maly[dlugosc]]+1;
  end;
  for znak:='a' to 'z' do
    writeln(znak, ' ', znaki[znak]);
  end;

end.  

它在condtion时失败了,我不知道为什么。 请求给我线索

2 个答案:

答案 0 :(得分:2)

一个char,napis [ 1 ];不能成为2个字符#13 &amp;的#10 ...

所以,我会这样做:

var
  znaki: array['a'..'z'] of integer = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  napis: String;
  maly: String;
  dlugosc: Integer;
  znak: char;


begin
napis := 'a';
while ((Length(napis) > 0)) do
begin
  readln(napis);
//  napis := StringReplace(napis, #13#10, #10, [rfReplaceAll]);  //useless for a console readln
  maly:=LowerCase(napis);
  for dlugosc:=(length(maly)) downto 1 do
  begin
    znaki[maly[dlugosc]]:=znaki[maly[dlugosc]]+1;
  end;
  for znak:='a' to 'z' do
    writeln(znak, ' ', znaki[znak]);
end;
end.

答案 1 :(得分:2)

#10是换行符

#13是回车(即移至行首)

您只需要检查#10。