1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

指定された位置に文字列を追加する

Posted at

文字列 S , T と、整数 N が与えられるので、 S の N 文字目の後ろに T を挿入した文字列を出力してください。

というプログラム問題をRubyで解きました。

str1 = gets.chomp
str2 = gets.chomp
num = gets.to_i
ans = ""
array1 = []
hcount = str1.size()
for i in 0...hcount do
    w_str = str1[i].to_s
    array1.push(w_str)
end

array1.insert(num,str2)
N = array1.size()
for j in 0...N do
    w_str2 = array1[j].to_s
    ans = ans + w_str2
end

puts ans

baseという文字列にballという文字列を文字列の4要素目に挿入しました。

ruby1.jpg

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?