標準入力からの入力に現れた数字をカウントする

明解C言語 入門編 > 8. いろいろなプログラムを作ってみよう >

標準入力からの入力に現れた数字をカウントする

Delphi


program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

procedure main();
var
i: Integer;
s: String;
cnt: array[0..9] of Integer;
begin
for i := 0 to 9 do
cnt[i] := 0;

while (not Eof) do
begin
readln(s);

for i := 0 to length(s) do
begin
case s[i] of
'0': inc(cnt[0]);
'1': inc(cnt[1]);
'2': inc(cnt[2]);
'3': inc(cnt[3]);
'4': inc(cnt[4]);
'5': inc(cnt[5]);
'6': inc(cnt[6]);
'7': inc(cnt[7]);
'8': inc(cnt[8]);
'9': inc(cnt[9]);
end;
end;

end;

for i := 0 to 9 do
writeln(format('"%d" : %d', [i, cnt[i]]));
end;

begin
main;
end.

実行結果

S:\>lesson065\Project1.exe
3.14159265
^Z
"0" : 0
"1" : 2
"2" : 1
"3" : 1
"4" : 1
"5" : 2
"6" : 1
"7" : 0
"8" : 0
"9" : 1