a = gets.chomp.chars count = 0 (a.length - 1).downto(0) do |i| count += 1 if a[i] =~ /[0-9]/ if count == 3 and i - 1 > 0 a[i + 1..a.length] = a[i..a.length - 1] a[i] = "," count = 0 end end puts a.join