Reversing a string without using array, classes and reverse function

Discussion in 'Ruby' started by Rubist Rohit, Apr 22, 2011.

  1. Rubist Rohit

    Rubist Rohit Guest

    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/.
    Rubist Rohit, Apr 22, 2011
    #1
    1. Advertising

  2. Rubist Rohit

    spiralofhope Guest

    Re: Reversing a string without using array, classes and reversefunction

    On Fri, 22 Apr 2011 12:54:16 +0900
    Rubist Rohit <> 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
    spiralofhope, Apr 22, 2011
    #2
    1. Advertising

  3. Rubist Rohit

    Rubist Rohit Guest

    Re: Reversing a string without using array, classes and reversefunction

    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/.
    Rubist Rohit, Apr 22, 2011
    #3
  4. Re: Reversing a string without using array, classes and reversefunction

    [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 <>

    > 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/.
    >
    >
    Jose Calderon-Celis, Apr 22, 2011
    #4
  5. Rubist Rohit

    Stu Guest

    Re: Reversing a string without using array, classes and reversefunction

    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
    <> 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/.
    >
    >
    Stu, Apr 22, 2011
    #5
  6. Rubist Rohit

    Stu Guest

    Re: Reversing a string without using array, classes and reversefunction

    On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis
    <> 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
    Stu, Apr 22, 2011
    #6
  7. Rubist Rohit

    Stu Guest

    Re: Reversing a string without using array, classes and reversefunction

    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 <> wrote:
    > On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis
    > <> 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
    >
    Stu, Apr 22, 2011
    #7
  8. Rubist Rohit

    Larry Lv Guest

    Re: Reversing a string without using array, classes and reversefunction

    [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 <> 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 <> wrote:
    > > On Fri, Apr 22, 2011 at 2:45 AM, Jose Calderon-Celis
    > > <> 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
    Larry Lv, Apr 22, 2011
    #8
  9. Rubist Rohit

    Robert Dober Guest

    Re: Reversing a string without using array, classes and reversefunction

    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.
    Robert Dober, Apr 22, 2011
    #9
  10. Re: Reversing a string without using array, classes and reversefunction

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

    --
    Posted via http://www.ruby-forum.com/.
    Hans Mackowiak, Apr 22, 2011
    #10
  11. Re: Reversing a string without using array, classes and reversefunction

    On Fri, Apr 22, 2011 at 12:54 PM, Rubist Rohit
    <> 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
    Harry Kakueki, Apr 22, 2011
    #11
  12. Rubist Rohit

    Rubist Rohit Guest

    Re: Reversing a string without using array, classes and reversefunction

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


    --
    Posted via http://www.ruby-forum.com/.
    Rubist Rohit, Apr 22, 2011
    #12
  13. Rubist Rohit

    spiralofhope Guest

    Re: Reversing a string without using array, classes and reversefunction

    On Fri, 22 Apr 2011 20:15:34 +0900
    Harry Kakueki <> 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. =)


    --
    http://spiralofhope.com
    spiralofhope, Apr 22, 2011
    #13
  14. Rubist Rohit

    Rubist Rohit Guest

    Re: Reversing a string without using array, classes and reversefunction

    @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/.
    Rubist Rohit, Apr 22, 2011
    #14
  15. Re: Reversing a string without using array, classes and reversefunction

    [Note: parts of this message were removed to make it a legal post.]

    @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 <>

    > What about
    >
    > y = lambda{ | str | str.empty? ? "" : y[ str[1..-1] ] + str[0] }
    >
    > Cheers
    > Robert
    >
    >
    >
    >
    Jose Calderon-Celis, Apr 22, 2011
    #15
  16. Re: Reversing a string without using array, classes and reversefunction

    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/.
    Brian Candler, Apr 23, 2011
    #16
  17. Rubist Rohit

    Robert Dober Guest

    Re: Reversing a string without using array, classes and reversefunction

    On Sat, Apr 23, 2011 at 1:11 PM, Brian Candler <> wrote:
    > I'm surprised nobody has yet shown using 'inject', as for once it makes

    that was mean ;)



    --
    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.
    Robert Dober, Apr 23, 2011
    #17
  18. Rubist Rohit

    Josh Cheek Guest

    Re: Reversing a string without using array, classes and reversefunction

    [Note: parts of this message were removed to make it a legal post.]

    On Thu, Apr 21, 2011 at 10:54 PM, Rubist Rohit <
    > 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 :)


    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 , s2[-i-1] = s2[-i-1] , s2 }
    s2 # => "54321"
    Josh Cheek, Apr 23, 2011
    #18
  19. Re: Reversing a string without using array, classes and reversefunction

    On Sun, 24 Apr 2011, Josh Cheek wrote:

    > On Thu, Apr 21, 2011 at 10:54 PM, Rubist Rohit <
    > > 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 :)


    [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! :D

    Much obliged!
    --
    Duke
    Duke Normandin, Apr 23, 2011
    #19
  20. Re: Reversing a string without using array, classes and reversefunction

    On Sat, Apr 23, 2011 at 7:33 PM, Josh Cheek <> 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. :)
    Adam Prescott, Apr 23, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin

    Help: 2D Array Reversing

    Kevin, Oct 3, 2003, in forum: C++
    Replies:
    4
    Views:
    993
    Thomas Matthews
    Oct 6, 2003
  2. dogbite
    Replies:
    4
    Views:
    691
    osmium
    Oct 10, 2003
  3. netforce

    reversing a string - newbie question

    netforce, Mar 2, 2004, in forum: C Programming
    Replies:
    9
    Views:
    530
    Old Wolf
    Mar 8, 2004
  4. arnuld
    Replies:
    8
    Views:
    364
    santosh
    Mar 11, 2007
  5. Tyler
    Replies:
    7
    Views:
    550
    James Kuyper
    Apr 9, 2012
Loading...

Share This Page