Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Reversing a string without using array, classes and reverse function

Reply
Thread Tools

Reversing a string without using array, classes and reverse function

 
 
Rubist Rohit
Guest
Posts: n/a
 
      04-22-2011
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/.

 
Reply With Quote
 
 
 
 
spiralofhope
Guest
Posts: n/a
 
      04-22-2011
On Fri, 22 Apr 2011 12:54:16 +0900
Rubist Rohit <(E-Mail Removed)> 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




--
http://spiralofhope.com


 
Reply With Quote
 
 
 
 
Rubist Rohit
Guest
Posts: n/a
 
      04-22-2011
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/.

 
Reply With Quote
 
Jose Calderon-Celis
Guest
Posts: n/a
 
      04-22-2011
[Note: parts of this message were removed to make it a legal post.]

#!/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 <(E-Mail Removed)>

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


 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      04-22-2011
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
<(E-Mail Removed)> 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/.
>
>


 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      04-22-2011
On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis
<(E-Mail Removed)> wrote:

> for j in 1..len do
> =A0mycommand +=3D 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 =3D "1234567890"
r=3DString.new
i =3D 1
while i <=3D s.length
r << s[i*(-1)]
i+=3D1
end
printf s
"0987654321"
=3D> "0987654321"
> y =3D=3D x.reverse

true

 
Reply With Quote
 
Stu
Guest
Posts: n/a
 
      04-22-2011
One more with ruby blocks (This would be the ruby way =3D) )

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

r =3D=3D s.reverse
true

cheers!!!

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

~Stu
On Fri, Apr 22, 2011 at 3:32 AM, Stu <(E-Mail Removed)> wrote:
> On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis
> <(E-Mail Removed)> wrote:
>
>> for j in 1..len do
>> =A0mycommand +=3D 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 =3D "1234567890"
> r=3DString.new
> i =3D 1
> while i <=3D s.length
> =A0r << s[i*(-1)]
> =A0i+=3D1
> end
> printf s
> "0987654321"
> =A0=3D> "0987654321"
>> y =3D=3D x.reverse

> true
>


 
Reply With Quote
 
Larry Lv
Guest
Posts: n/a
 
      04-22-2011
[Note: parts of this message were removed to make it a legal post.]

#!/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 <(E-Mail Removed)> 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 <(E-Mail Removed)> wrote:
> > On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis
> > <(E-Mail Removed)> 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

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      04-22-2011
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.

 
Reply With Quote
 
Hans Mackowiak
Guest
Posts: n/a
 
      04-22-2011
why without reverse functions?
like:
"abc".each_char.reverse_each {|c| p c}

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reversing order of words in a given string Kelly B C Programming 2 04-26-2007 02:52 PM
String reversing problem Albert C Programming 46 01-04-2006 10:49 AM
Reversing a folder path in a string Lindsay C++ 3 07-18-2005 05:09 AM
reversing a string - newbie question netforce C Programming 9 03-08-2004 12:04 AM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM



Advertisments