Reversing a string without using array, classes and reverse function

I am trying this:

mystring = gets
mystring.scan(/..$/) {|x| puts x}

It returns only the last character. Is it possible to add the above line
in loop?

···

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

Here's something I stumbled through which seems to work.

- Using a regex of /.$/
- Slowly chomping away at the original string.
- Using another variable to build my result.

mystring = 'Hello, World!'
result = ''

fail = 0
until fail == "100" or mystring == '' do
  fail += 1
  mystring.match( %r{(.$)} )
  break if $~ == nil
  result += $~[1]
  mystring = mystring.chomp( $~[1] )
end

puts result

···

On Fri, 22 Apr 2011 12:54:16 +0900 Rubist Rohit <passionate_programmer@hotmail.com> wrote:

I am trying this:

mystring = gets
mystring.scan(/..$/) {|x| puts x}

It returns only the last character. Is it possible to add the above
line in loop?

--
http://spiralofhope.com

Simple =)

s = "1234567890"
r=String.new
i = 1; while i <= s.length
  r << s[-i]
  i+=1
end

r == s.reverse
true

r is now the reverse of s

···

On Thu, Apr 21, 2011 at 10:54 PM, Rubist Rohit <passionate_programmer@hotmail.com> wrote:

I am trying this:

mystring = gets
mystring.scan(/..$/) {|x| puts x}

It returns only the last character. Is it possible to add the above line
in loop?

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

What about

y = lambda{ | str | str.empty? ? "" : y[ str[1..-1] ] + str[0] }

Cheers
Robert

···

--
The 1,000,000th fibonacci number contains '42' 2039 times; that is
almost 30 occurrences more than expected (208988 digits).
N.B. The 42nd fibonacci number does not contain '1000000' that is
almost the expected 3.0e-06 times.

why without reverse functions?
like:
"abc".each_char.reverse_each {|c| p c}

···

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

s = "I am a string."

t = ""
s.each_char{|f| t.insert(0,f)}
p t #> ".gnirts a ma I"

Harry

···

On Fri, Apr 22, 2011 at 12:54 PM, Rubist Rohit <passionate_programmer@hotmail.com> wrote:

I am trying this:

mystring = gets
mystring.scan(/..$/) {|x| puts x}

It returns only the last character. Is it possible to add the above line
in loop?

--

Thought I'd join the fun :slight_smile:

s = "12345"

# using reverse
s.reverse # => "54321"

# using each_char
s2 = ""
s.each_char { |char| s2 = char << s2 }
s2 # => "54321"

# using scan
s2 = ""
s.scan(/./) { |char| s2 = char << s2 }
s2 # => "54321"

# using unix (this doesn't escape all chars properly, though)
`rev <<<#{s.inspect}`.chomp # => "54321"

# using recursion
rev = lambda do |string|
  if string.empty?
    ""
  else
    rev.call(string[1..-1]) << string[0,1]
  end
end
rev.call s # => "54321"

# exchanging chars
s2 = s.dup
s2.length./(2).times { |i| s2[i] , s2[-i-1] = s2[-i-1] , s2[i] }
s2 # => "54321"

···

On Thu, Apr 21, 2011 at 10:54 PM, Rubist Rohit < passionate_programmer@hotmail.com> wrote:

I am trying this:

mystring = gets
mystring.scan(/..$/) {|x| puts x}

It returns only the last character. Is it possible to add the above line
in loop?

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

I attempted below given code, but it is neither displaying result nor
error:

================CODE==========================
s = "This is to test reverse of a string"
len = s.length
for j in len..1 do
  mycommand = "s.scan(/.$/) {|x| puts x}"
  mycommand = mycommand.insert 7,"."
end

···

==============================================

What I am doing is to insert a period (.) in the seventh or eighth
position on each loop.

spiralofhope wrote in post #994433:

On Fri, 22 Apr 2011 12:54:16 +0900

I am trying this:

mystring = gets
mystring.scan(/..$/) {|x| puts x}

It returns only the last character. Is it possible to add the above
line in loop?

Here's something I stumbled through which seems to work.

- Using a regex of /.$/
- Slowly chomping away at the original string.
- Using another variable to build my result.

mystring = 'Hello, World!'
result = ''

fail = 0
until fail == "100" or mystring == '' do
  fail += 1
  mystring.match( %r{(.$)} )
  break if $~ == nil
  result += $~[1]
  mystring = mystring.chomp( $~[1] )
end

puts result

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

Oh, of course! This makes a lot of sense to me..

Some other examples using .length were also very easy on my brain. =)

···

On Fri, 22 Apr 2011 20:15:34 +0900 Harry Kakueki <list.push@gmail.com> wrote:

s = "I am a string."

t = ""
s.each_char{|f| t.insert(0,f)}
p t #> ".gnirts a ma I"

--
http://spiralofhope.com

@Harry,

That was excellent. Very little code and fits to my condition of not
using arrays as well.

Harry Kakueki wrote in post #994483:

···

On Fri, Apr 22, 2011 at 12:54 PM, Rubist Rohit

s = "I am a string."

t = ""
s.each_char{|f| t.insert(0,f)}
p t #> ".gnirts a ma I"

Harry

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

@Robert: Thanks, Cheers

#!/usr/bin/env ruby

···

#
require 'rubygems'

y = lambda{ | str |
# puts " #{str}"
  str.empty? ? "" : y[ str[1..-1] ] + str[0]
}

s="1234567890"
p s
p y.call(s)

output:./reverseL.rb
"1234567890"
"0987654321"

---
Jose Calderon-Celis

2011/4/22 Robert Dober <robert.dober@gmail.com>

What about

y = lambda{ | str | str.empty? ? "" : y[ str[1..-1] ] + str[0] }

Cheers
Robert

[snip the good stuff]

Now that's what I call a _constructive- reply! All good stuff for
Ruby noobs! While some others here are going into my 'killfile', you
are a rising *star*. :))

It's like a long-time friend of mine used to say - may she rest in
peace! - opinions and advice are like assholes! Everybody's got one!
It's the _informed_ opinions and advice that I'm after - and you seem
to have it! All the other bullshit is going into my bit-bucket - not
now! but right now! :smiley:

Much obliged!

···

On Sun, 24 Apr 2011, Josh Cheek wrote:

On Thu, Apr 21, 2011 at 10:54 PM, Rubist Rohit < > passionate_programmer@hotmail.com> wrote:

> I am trying this:
>
> mystring = gets
> mystring.scan(/..$/) {|x| puts x}
>
> It returns only the last character. Is it possible to add the above line
> in loop?
>
> --
> Posted via http://www.ruby-forum.com/\.
>
>
Thought I'd join the fun :slight_smile:

--
Duke

I feel a benchmark coming, of all the suggestions so far. :slight_smile:

···

On Sat, Apr 23, 2011 at 7:33 PM, Josh Cheek <josh.cheek@gmail.com> wrote:

# using reverse
# using each_char
# using scan
# using unix (this doesn't escape all chars properly, though)
# using recursion
# exchanging chars

#!/usr/bin/env ruby

···

#
require 'rubygems'

puts "ruby #{RUBY_VERSION}"
s = "This is to test reverse of a string"
p s
len=s.length
mycommand=""
for j in 1..len do
  mycommand += s[-1*j]
end
p mycommand
Jose Calderon-Celis
5199906-7970
http://www.tm.com.pe/mensajes/

2011/4/22 Rubist Rohit <passionate_programmer@hotmail.com>

I attempted below given code, but it is neither displaying result nor
error:

================CODE==========================
s = "This is to test reverse of a string"
len = s.length
for j in len..1 do
mycommand = "s.scan(/.$/) {|x| puts x}"
mycommand = mycommand.insert 7,"."
end

What I am doing is to insert a period (.) in the seventh or eighth
position on each loop.

spiralofhope wrote in post #994433:
> On Fri, 22 Apr 2011 12:54:16 +0900
>> I am trying this:
>>
>> mystring = gets
>> mystring.scan(/..$/) {|x| puts x}
>>
>> It returns only the last character. Is it possible to add the above
>> line in loop?
>
> Here's something I stumbled through which seems to work.
>
> - Using a regex of /.$/
> - Slowly chomping away at the original string.
> - Using another variable to build my result.
>
> mystring = 'Hello, World!'
> result = ''
>
> fail = 0
> until fail == "100" or mystring == '' do
> fail += 1
> mystring.match( %r{(.$)} )
> break if $~ == nil
> result += $~[1]
> mystring = mystring.chomp( $~[1] )
> end
>
> puts result

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

I'm surprised nobody has yet shown using 'inject', as for once it makes
sense here.

a = "foobar"
puts a.each_char.inject("") { |str,chr| chr+str } # raboof

···

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

for j in 1..len do
mycommand += s[-1*j]

very nice I like the use of your math. By multiplying the array index
by negative one your sending the index a reverse number while your for
loop traverses forward. This is perfect logic.

Here is a rewrite with my while loop:

s = "1234567890"
r=String.new
i = 1
while i <= s.length
  r << s[i*(-1)]
  i+=1
end
printf s
"0987654321"
=> "0987654321"

y == x.reverse

true

···

On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis <josecalderoncelis@gmail.com> wrote:

Thanks all for brilliant examples.

@Jose: Thanks so much. That was too close.

Jose Calderon-Celis wrote in post #994451:

···

#!/usr/bin/env ruby
#
require 'rubygems'

puts "ruby #{RUBY_VERSION}"
s = "This is to test reverse of a string"
p s
len=s.length
mycommand=""
for j in 1..len do
  mycommand += s[-1*j]
end
p mycommand
Jose Calderon-Celis
5199906-7970
http://www.tm.com.pe/mensajes/

2011/4/22 Rubist Rohit <passionate_programmer@hotmail.com>

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

that was mean :wink:

···

On Sat, Apr 23, 2011 at 1:11 PM, Brian Candler <b.candler@pobox.com> wrote:

I'm surprised nobody has yet shown using 'inject', as for once it makes

--
The 1,000,000th fibonacci number contains '42' 2039 times; that is
almost 30 occurrences more than expected (208988 digits).
N.B. The 42nd fibonacci number does not contain '1000000' that is
almost the expected 3.0e-06 times.

One more with ruby blocks (This would be the ruby way =) )

s = "1234567890"
r = String.new
s.length.times{|i| r << s[(i+1)*(-1)]}

r == s.reverse
true

cheers!!!

That was fun!!!... give me another =)

~Stu

···

On Fri, Apr 22, 2011 at 3:32 AM, Stu <stu@rubyprogrammer.net> wrote:

On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis > <josecalderoncelis@gmail.com> wrote:

for j in 1..len do
mycommand += s[-1*j]

very nice I like the use of your math. By multiplying the array index
by negative one your sending the index a reverse number while your for
loop traverses forward. This is perfect logic.

Here is a rewrite with my while loop:

s = "1234567890"
r=String.new
i = 1
while i <= s.length
r << s[i*(-1)]
i+=1
end
printf s
"0987654321"
=> "0987654321"

y == x.reverse

true

#!/usr/bin/env ruby

while line = gets.chomp
  r = String.new
  (1..line.length).each do |i|
    r << line[-1*i]
  end
  puts r
  puts r == line.reverse
end

···

On Fri, Apr 22, 2011 at 4:43 PM, Stu <stu@rubyprogrammer.net> wrote:

One more with ruby blocks (This would be the ruby way =) )

s = "1234567890"
r = String.new
s.length.times{|i| r << s[(i+1)*(-1)]}

r == s.reverse
true

cheers!!!

That was fun!!!... give me another =)

~Stu
On Fri, Apr 22, 2011 at 3:32 AM, Stu <stu@rubyprogrammer.net> wrote:
> On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis > > <josecalderoncelis@gmail.com> wrote:
>
>> for j in 1..len do
>> mycommand += s[-1*j]
>>
>>
>
> very nice I like the use of your math. By multiplying the array index
> by negative one your sending the index a reverse number while your for
> loop traverses forward. This is perfect logic.
>
> Here is a rewrite with my while loop:
>
> s = "1234567890"
> r=String.new
> i = 1
> while i <= s.length
> r << s[i*(-1)]
> i+=1
> end
> printf s
> "0987654321"
> => "0987654321"
>> y == x.reverse
> true
>

--
Best Regards,

*Larry Lv*
@ Baidu NLP