Checking if character is newline

瘦欲@ 提交于 2019-12-10 18:30:54

问题


Hello i'v got simple program which is couting characters in given text until line is empty line only with new line

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.  

it fails on while condtion and i dont know why. Pleas give me clue


回答1:


One char, napis[1]; can't be 2 chars #13 & #10...

So, I'll do this for example:

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.



回答2:


#10 is line feed

#13 is carriage return (i.e. move to beginning of line)

You only need to check for #10.



来源:https://stackoverflow.com/questions/10598365/checking-if-character-is-newline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!