Insert letters of the alphabet between the original letters of a string

Hi All.

What I seek to realize is that having a particular string, I insert
letters of the alphabet between the letters of the original string.

For example if I have the word: STRING ==> SATBRCIDG
                                            - - - -
This is the code he was doing.

···

######################################################

a = %w{a b c d e f g h i j k l m n o p q r s t u v w x y z}
str=""

puts "Enter a string:: "
str=gets.chomp.to_s

for i in 0..a.length
  s=cad.insert(i+2, "#{a[i]}")
end

puts s
#########################################################

Thanks.

--
Posted via http://www.ruby-forum.com/.

"STRING".each_char.zip(("A".."Z").cycle).join[0..-2]
#> "SATBRCIDNEG"

···

--
Posted via http://www.ruby-forum.com/.

Hi,

What's "cad"? It isn't defined anywhere.

Apart from that, the calculation of the indices for insertion is wrong.
You start with 2 instead of 1, and you don't take into account that the
string is growing with each step.

The indices have to be 1, 3, 5, 7, ...

# repeat the characters infintely, because the string may have any
length
alphabet = ('a'..'z').cycle

string = "String"
0.upto string.length - 2 do |i|
  string.insert 2 * i + 1, alphabet.next
end
puts string

But like I already said in your last thread: Ruby isn't Java. You need
to get rid of this strange "for" loop.

···

--
Posted via http://www.ruby-forum.com/.

What I seek to realize is that having a particular string, I insert
letters of the alphabet between the letters of the original string.

try eg,

"string".split(//).zip("abcde".split(//)).join

=> "satbrcidneg"

kind regards -botp

···

On Thu, Jul 5, 2012 at 1:56 AM, Joao Silva <lists@ruby-forum.com> wrote:

What I seek to realize is that having a particular string, I insert
letters of the alphabet between the letters of the original string.

For example if I have the word: STRING ==> SATBRCIDG
                                            - - - -

I'm surprised no one tried something like this.
Well, not really :slight_smile:

Not pretty.

s = "STRING"
a = ("a".."z").cycle.take(s.size).join

u = s.size
p (s+a).unpack("a"+("x"*(u-1)+"a"+("X"*u)+"a")*(u-1))*""

Harry

Information: cycle and zip does not work on ruby1.8 :smiley:

···

--
Posted via http://www.ruby-forum.com/.

"string".split(//).zip("abcde".split(//)).join

=> "satbrcidneg"

somehow, this seems readable and easy to type,

"string".each_char.zip("abcde".each_char).join

=> "satbrcidneg"

kind regards -botp

···

On Thu, Jul 5, 2012 at 11:02 AM, botp <botpena@gmail.com> wrote:

I prefer String#chars to String#each_char when not passing a block,
and String#chop to remove the last character.

   "STRING".chars.zip((?A..?Z).cycle).join.chop

···

On 07/04/2012 08:27 PM, Hans Mackowiak wrote:

"STRING".each_char.zip(("A".."Z").cycle).join[0..-2]

--
Lars Haugseth

info: upgrade. Plans for 1.8.7

best regards -botp

···

On Thu, Jul 5, 2012 at 5:24 PM, Hans Mackowiak <lists@ruby-forum.com> wrote:

Information: cycle and zip does not work on ruby1.8 :smiley:

zip is available for 1.8

Anyway, I think this will work on 1.8 and 1.9

s = "STRING"
a = ("a".."z").to_a

t = s.size-1
t.downto(1).each{|x| s.insert(x,a[x%26-1])}
p s

Harry

···

On Thu, Jul 5, 2012 at 6:24 PM, Hans Mackowiak <lists@ruby-forum.com> wrote:

Information: cycle and zip does not work on ruby1.8 :smiley: