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?

On Fri, 22 Apr 2011 12:54:16 +0900
Rubist R. [email protected] 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?

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

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

#!/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 R. [email protected]

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 R.

On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis
[email protected] 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

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

#!/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 [email protected] wrote:

while i <= s.length


Best Regards,

Larry Lv
@ Baidu NLP

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

What about

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

Cheers
Robert

On Fri, Apr 22, 2011 at 12:54 PM, Rubist R.
[email protected] 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?

s = “I am a string.”

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

Harry

On Fri, 22 Apr 2011 20:15:34 +0900
Harry K. [email protected] wrote:

s = “I am a string.”

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

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

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

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 R. [email protected]

@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 D. [email protected]

@Harry,

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

Harry K. wrote in post #994483:

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

s = “I am a string.”

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

Harry

On Sat, Apr 23, 2011 at 1:11 PM, Brian C. [email protected]
wrote:

I’m surprised nobody has yet shown using ‘inject’, as for once it makes
that was mean :wink:

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

On Thu, Apr 21, 2011 at 10:54 PM, Rubist R. <
[email protected]> wrote:

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 Sun, 24 Apr 2011, Josh C. wrote:


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

Thought I’d join the fun :slight_smile:

[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 Sat, Apr 23, 2011 at 7:33 PM, Josh C. [email protected]
wrote:

using reverse

using each_char

using scan

using unix (this doesn’t escape all chars properly, though)

using recursion

exchanging chars

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