ForNext
Only Do What Only You Can Do
074. 文字列内の数字の出現回数を数える
VBScript
JScript
Perl
更新日 : 2010.10.18
@cnt = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0); $str = "3.1415926535897932846"; foreach $char(split(//, $str)) { $cnt[$char]++ if (("0" le $char) && ($char le "9")); } $i = 0; foreach $char(@cnt) { printf("'%d' : %d\n", $i++, $char); }
L:\>perl lesson_09_074.pl '0' : 0 '1' : 2 '2' : 2 '3' : 3 '4' : 2 '5' : 3 '6' : 2 '7' : 1 '8' : 2 '9' : 3
PHP
更新日 : 2010.11.03
<?php $cnt = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); $str = "3.1415926535897932846"; for ($i = 0; $i < strlen($str); $i++) { $char = substr($str, $i, 1); if (("0" <= $char) && ($char <= "9")) $cnt[$char]++; } $i = 0; foreach ($cnt as $char) { printf("'%d' : %d\n", $i++, $char); } ?>
L:\>php lesson_09_074.php '0' : 0 '1' : 2 '2' : 2 '3' : 3 '4' : 2 '5' : 3 '6' : 2 '7' : 1 '8' : 2 '9' : 3
Python
更新日 : 2010.11.17
cnt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] str = "3.1415926535897932846" for i in range(len(str)): char = str[i] if (("0" <= char) and (char <= "9")): cnt[int(char)] += 1 i = 0; for char in cnt: print "'%d' : %d" % (i, char) i += 1
N:\>python lesson_09_074.py '0' : 0 '1' : 2 '2' : 2 '3' : 3 '4' : 2 '5' : 3 '6' : 2 '7' : 1 '8' : 2 '9' : 3
Ruby
更新日 : 2010.11.01
cnt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] str = "3.1415926535897932846" str.split(//s).each do |char| cnt[char.to_i] += 1 if (("0" <= char) && (char <= "9")) end cnt.each_with_index do |char, i| printf("'%d' : %d\n", i, char) end
L:\>ruby l:\lesson_09_074.rb '0' : 0 '1' : 2 '2' : 2 '3' : 3 '4' : 2 '5' : 3 '6' : 2 '7' : 1 '8' : 2 '9' : 3
PowerShell
Scala
F#
C
更新日 : 2010.10.08
#include <stdio.h> int main(int argc, char* argv[]) { int cnt[10] = {0}; char str[100] = "3.1415926535897932846"; unsigned i = 0; while (str[i]) { if (('0' <= str[i]) && (str[i] <= '9')) cnt[str[i] - '0']++; i++; } for (i = 0; i < 10; i++) { printf("'%d' : %d\n", i, cnt[i]); } return 0; }
R:\>lesson074\Project1.exe '0' : 0 '1' : 2 '2' : 2 '3' : 3 '4' : 2 '5' : 3 '6' : 2 '7' : 1 '8' : 2 '9' : 3
C++
C++Builder
VC++
C#
Java
更新日 : 2010.11.05
class Lesson074 { public static void main(String[] args) { int[] cnt = new int[10]; String str = "3.1415926535897932846"; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (('0' <= ch) && (ch <= '9')) cnt[ch - '0']++; } for (int i = 0; i < 10; i++) System.out.printf("'%d' : %d\n", i, cnt[i]); } }
L:\>java Lesson074 '0' : 0 '1' : 2 '2' : 2 '3' : 3 '4' : 2 '5' : 3 '6' : 2 '7' : 1 '8' : 2 '9' : 3
Objective-C
D
VB
VB.NET
Delphi
更新日 : 2010.09.24
program Project1; {$APPTYPE CONSOLE} uses SysUtils; procedure main(); var i, j: Integer; str: String; cnt: array[0..9] of Integer; begin str := '3.1415926535897932846'; for i := 0 to 9 do cnt[i] := 0; for i := 1 to Length(str) do begin if ('0' <= str[i]) and (str[i] <= '9') then begin j := Integer(str[i]) - Integer('0'); Inc(cnt[j]); end; end; for i := 0 to 9 do Writeln(Format('"%d" : %d', [i, cnt[i]])); end; begin main; end.
S:\>lesson074\Project1.exe "0" : 0 "1" : 2 "2" : 2 "3" : 3 "4" : 2 "5" : 3 "6" : 2 "7" : 1 "8" : 2 "9" : 3